0

I have used this link to get me started, [https://stackoverflow.com/a/11056698/5609063][1]

I am trying to create an object which creates its GUI internally, and cleans itself up.

I have a call to quit() as well within lets me close the gui out if I want to force a close or I can just sit on .exec call until the main window is closed.

FooObj.h

class FooObj
{
public:
    FooObj(FooObj *obj);
    ~FooObj();
private:
    GUIApp *guiApp;
    FooObj *m_fooObj;
}

FooObj.c

FooObj::FooObj()
{ 
    guiApp = new GUIApp(this);
}

FooObj::~FooObj()
{
    delete guiApp;
}

GUIApp.h

class GUIApp
{
public:
    GUIApp(FooObj *obj);
    ~GUIApp();

private:
    std::thread m_appThread;
    void m_RunApp();
}

GUIApp.c

GUIApp::GUIApp(obj)
{
    m_fooObj = obj;
    m_appThread = std::thread ([this] {m_RunApp();});
}

GUIApp::~GUIApp()
{
    if(m_appThread.joinable())
        m_appThread.join();
}

void GUIApp::m_RunApp()
{
    int argc = 1;
    auto a = new QApplication(argc, NULL);
    auto win = new FooObjGUI(m_fooObj);
    win->show();
    a->exec();
    return;
}

In GUIApp, there is a call to FooObjGUI which uses FooObj to a gui setup to handle all the data I want to display and what actions I expose to the user.

My intention is to have the user of this code not have to worry about qt and to have it clean itself up.

This code works fine in a dll or exe when I create a single FooObj. I cannot create a second FooObj because I cannot create a second QApplication. I have tried several ideas starting the a->exec() in a separate thread, but the events are not handled and the gui sits idle.

Is there a way to call QApplication across multiple threads within a single dll? Is there a workaround to have both FooObj use the same QApplication and not block the execution of the dll? The only workaround I have for myself at the moment is use 2 copies of the dll, but that is not a good longterm solution.

  • I don't believe what you are trying is possible. You will have to use QProcess to start your application multiple times instead. – drescherjm Oct 15 '17 at 17:34
  • Are you saying to start a process instead of a thread in the GUIApp constructor? – CodeSlapper Oct 15 '17 at 17:53
  • Are you sure you need multiple QApplication? Maybe QEventLoop will be enough for your GUIs? P.S.: You can't create multiple QApplications in one process – Xplatforms Oct 16 '17 at 08:07
  • Do you have an example of QEventLoop? I really only need one qapplication across the multiple windows to handle my gui code. – CodeSlapper Oct 16 '17 at 14:05
  • I got a solution for my prob that is suitable. I still created a seperate thread GUIApp constructor. That thread creates a new FooObjGUI each time I call the GUIApp constructor in a loop. Once I create all the objects I plan on using I set some flag to stop executing that loop. Then I enter the a->exec() call. In the destructor I join the separate thread once and perform cleanup on the items I added for keeping track of how many FooObjGUI objects I created. I can kill the app by closing all the windows or by calling quit. – CodeSlapper Nov 06 '17 at 18:34

0 Answers0