Results 1 to 4 of 4

Thread: Launching Multiple QApplication within a DLL

  1. #1
    Join Date
    Mar 2017
    Posts
    9
    Qt products
    Qt5
    Platforms
    Windows

    Default Launching Multiple QApplication within a DLL

    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.

  2. #2
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,714
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: Launching Multiple QApplication within a DLL

    s there a way to call QApplication across multiple threads within a single dll?
    No, and it has to do with the event loop, QApplication is designed as a singleton.

    What you can do is spawn separate processes using QProcess.
    You wont have a direct access to their events (as you mentioned you need) but you can overcome it the usual way for this kind of a scenario by using an IPC mechanism of your choice.
    This will involve code that inspects the events and dispatches them (or rather, data about them) over the IPC.

    Or, you could go about implementing your own "application" object building it around a QEventLoop - not something for beginners.

    However, reading your post I am not sure you actually need multiple QApplications, what you seem to need is merely "multiple UI's."
    I am putting quotes because you only really have one UI per QApplication - which can be made of many views and windows etc.
    You could have threads each doing his thing, and talking to a different window or UI - and all these threads can share the even processing of your QApplication, each thread can be killed or spawned.
    When the last thread exists, you can close your QApplication too.
    To be clear:
    your threads will drive the business logic, but they will be talking to a UI living the in the QApplication gui thread.

    None of the above options are trivial, but the last one with threads seems to me the most sensible, and probably the most practical to implement.
    Last edited by high_flyer; 16th October 2017 at 00:52.
    ==========================signature=============== ==================
    S.O.L.I.D principles (use them!):
    https://en.wikipedia.org/wiki/SOLID_...iented_design)

    Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.

  3. #3
    Join Date
    Mar 2017
    Posts
    9
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Launching Multiple QApplication within a DLL

    Thanks for the input high_flyer.

    I get what you are saying, and I have tried to implement it to some extent, but ran into issues.

    Options I see.
    1. Can I use a Qthread.exec() to run the guis instead of QApplication?
    2. Can I use a common thread to create the windows and qapplication for all the classes?
    3. Can I make my own processevents() loop?
    4. Can I use QEventLoop?

    Are there any examples of doing this out there?

  4. #4
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,714
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: Launching Multiple QApplication within a DLL

    1. Can I use a Qthread.exec() to run the guis instead of QApplication?
    No, the UI has to live in the QApplication UI main thread (the usual case, as I said, you could implement your own QApplication but do you really want/need to?)
    You could have your thread instruct creation of UI's in the main thread however.

    2. Can I use a common thread to create the windows and qapplication for all the classes?
    You already have that - the main UI thread.

    3. Can I make my own processevents() loop?
    4. Can I use QEventLoop?
    Yes, by using QEventLoop.

    Are there any examples of doing this out there?
    I don't know (maybe someone else here does).
    I would look in QDialog for example, (there are some other classes which have their own event loop as well) and try to get the right way of using it from there.
    A short google search provided this which I think might be interesting as well:
    https://www.qtdeveloperdays.com/2013...Event_Loop.pdf
    ==========================signature=============== ==================
    S.O.L.I.D principles (use them!):
    https://en.wikipedia.org/wiki/SOLID_...iented_design)

    Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.

Similar Threads

  1. Replies: 2
    Last Post: 1st August 2011, 07:30
  2. Segmentation fault after Launching Qt app
    By payal in forum Qt Programming
    Replies: 2
    Last Post: 3rd March 2010, 20:05
  3. Launching a PDF in platform independant way
    By JPNaude in forum Qt Programming
    Replies: 2
    Last Post: 30th September 2008, 20:04
  4. Launching external web browser from within Qt App
    By JimDaniel in forum Qt Programming
    Replies: 1
    Last Post: 31st October 2007, 19:22
  5. Launching problem
    By Salazaar in forum Newbie
    Replies: 5
    Last Post: 29th October 2007, 12:18

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.