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

Qt Code:
  1. class FooObj
  2. {
  3. public:
  4. FooObj(FooObj *obj);
  5. ~FooObj();
  6. private:
  7. GUIApp *guiApp;
  8. FooObj *m_fooObj;
  9. }
To copy to clipboard, switch view to plain text mode 
FooObj.c
Qt Code:
  1. FooObj::FooObj()
  2. {
  3. guiApp = new GUIApp(this);
  4. }
  5.  
  6. FooObj::~FooObj()
  7. {
  8. delete guiApp;
  9. }
To copy to clipboard, switch view to plain text mode 
GUIApp.h
Qt Code:
  1. class GUIApp
  2. {
  3. public:
  4. GUIApp(FooObj *obj);
  5. ~GUIApp();
  6.  
  7. private:
  8. std::thread m_appThread;
  9. void m_RunApp();
  10. }
To copy to clipboard, switch view to plain text mode 
GUIApp.c

Qt Code:
  1. GUIApp::GUIApp(obj)
  2. {
  3. m_fooObj = obj;
  4. m_appThread = std::thread ([this] {m_RunApp();});
  5. }
  6.  
  7. GUIApp::~GUIApp()
  8. {
  9. if(m_appThread.joinable())
  10. m_appThread.join();
  11. }
  12.  
  13. void GUIApp::m_RunApp()
  14. {
  15. int argc = 1;
  16. auto a = new QApplication(argc, NULL);
  17. auto win = new FooObjGUI(m_fooObj);
  18. win->show();
  19. a->exec();
  20. return;
  21. }
To copy to clipboard, switch view to plain text mode 
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.