Results 1 to 9 of 9

Thread: Starting QT GUI in a seperate Thread

  1. #1
    Join Date
    Sep 2008
    Posts
    13
    Thanks
    2
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11

    Default Starting QT GUI in a seperate Thread

    Hello everyone,

    I am continuing my talk from another thread and thought that this is a whole other issue in itself.

    I would like to start my main application and create two threads, one to process some data and one to initialize and start the QT GUI. (Why I don't just make my GUI the core of the application is another story).

    I am able to do this and compile everything, however, the GUI Thread seems to take over and all other threads seem to be blocked (or not getting any cpu time), including the main thread.

    Here's a sample of my code:

    Qt Code:
    1. void *initUI(char *argv[]) {
    2. hpgeUI(argv); //this function does all the QApplication, exec() stuff
    3. return 0;
    4. }
    5.  
    6. void *process() {
    7. printf("This is the process thread\n");
    8. return 0;
    9. }
    10.  
    11. int main(int argc, char *argv[])
    12. {
    13.  
    14. pthread_t guithread, process;
    15. pthread_create(&guithread, NULL, initUI(argv), 0);
    16. //CHECK PT 1
    17. pthread_create(&process, NULL, process, 0);
    18. while(1)
    19. {
    20. //do something here
    21. }
    22. return 0;
    23. }
    To copy to clipboard, switch view to plain text mode 

    Any ideas as to why this is happening? The GUI is showing up and seems to hijack all the resources of the application; no other threads are created and CHECK PT 1 is not reached.

    Regards,
    BB

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Starting QT GUI in a seperate Thread

    Any particular reason for using pthreads instead of QThread? Why not put your while loop in a worker thread (or get rid of it which is probably possible) and process the gui in the main thread?

  3. #3
    Join Date
    Aug 2008
    Location
    Ukraine, Krivoy Rog
    Posts
    1,963
    Thanked 370 Times in 336 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Starting QT GUI in a seperate Thread

    you can't create widgets in a non-GUI thread, the GUI-thread is main the thread. you need to send events or signals to main thread from another threads to create some widget.

  4. #4
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Starting QT GUI in a seperate Thread

    Actually the GUI thread is the thread the application object lives in. It doesn't have to be the "main" thread.

  5. #5
    Join Date
    Sep 2008
    Posts
    13
    Thanks
    2
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11

    Default Re: Starting QT GUI in a seperate Thread

    Quote Originally Posted by wysota View Post
    Actually the GUI thread is the thread the application object lives in. It doesn't have to be the "main" thread.
    From experience this looks to be true, however, the documentation does say that
    the QCoreApplication::exec() must always be called from the main thread (the thread that executes main())
    Is this because the QCoreApplicationtakes over all other threads and manages a subset of it's own? Meaning any other thread that is running outside or created outside of the QCoreApplicationis blocked? That is what seems to be going on.

    And to clear things up, I'm not creating widgets or doing any other graphical manipulation in the other threads. I am creating a thread to start the QApplicationand all the related graphical elements, then starting a second thread that does some processing.

    Quote Originally Posted by wysota View Post
    Any particular reason for using pthreads instead of QThread? Why not put your while loop in a worker thread (or get rid of it which is probably possible) and process the gui in the main thread?
    The reason why I don't just create a QThreadwithin QApplicationis because I am not able to modify the code that goes into this "processing" thread, except for the starting of the new thread and passing a few functions to pipe data through. It is a large C program which will be built outside of all the GUI stuff and I've created a static library for the GUI itself. So far it works except for the issue above.

    Thank you for all your help.

    Regards,
    BB

  6. #6
    Join Date
    Oct 2008
    Location
    Brazil, Sao Paulo - SP
    Posts
    17
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Starting QT GUI in a seperate Thread

    Got it!

    The CP 1 will never be reached because when calling pthread_create for the 1st time, you're not passing initUI to the function that will create the thread, you're CALLING it!!!

    The right code would be like this:
    Qt Code:
    1. pthread_create(&guithread, NULL, initUI, argv);
    To copy to clipboard, switch view to plain text mode 

    Passing initUI(argv) to pthread_create does not pass the pointer to the function initUI, but its resultant void* by effectively calling initUI(argv).

    Best regards

  7. The following user says thank you to tinsuke for this useful post:

    bbui210 (17th October 2008)

  8. #7
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Starting QT GUI in a seperate Thread

    Quote Originally Posted by bbui210 View Post
    From experience this looks to be true, however, the documentation does say that (...)
    It's been known to work fine.

    The reason why I don't just create a QThreadwithin QApplicationis because I am not able to modify the code that goes into this "processing" thread, except for the starting of the new thread and passing a few functions to pipe data through. It is a large C program which will be built outside of all the GUI stuff and I've created a static library for the GUI itself. So far it works except for the issue above.
    There are different design concepts available, so you can surely do it in another way. Your GUI can even be a separate process to the data processor and in this situation that would probably be the best solution.

  9. #8
    Join Date
    Jun 2018
    Posts
    2
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11

    Default Re: Starting QT GUI in a seperate Thread

    Hi!
    I am trying to do almost the same thing as @bbui210 but I am using std::thread. I am facing the issue that it is behaving sequential and not parallel. My QT GUI (QT application) is in the main thread. My code structure is like below-

    Qt Code:
    1. int process(arguments)
    2. {
    3. //processing and saving the data in a file
    4. }
    5.  
    6. int main(arguments)
    7. {
    8. //GUI is called which accesses the file
    9. std::thread threadObj(process, arguments_of_process);
    10. threadObj.join();
    11. }
    To copy to clipboard, switch view to plain text mode 

    I want my GUI to keep updating itself as file changes. My main reason for using std::thread was that it was easier to use and also I had to pass arguments to the function. Can someone tell me what is wrong with my code?

    Thanks!!

  10. #9
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,230
    Thanks
    302
    Thanked 864 Times in 851 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Starting QT GUI in a seperate Thread

    This isn't code, it's pseudocode. If you want us to help diagnose problems with code, post a minimum working example of REAL code that demonstrates the problem. Otherwise, you are just wasting everyone's time.
    <=== The Great Pumpkin says ===>
    Please use CODE tags when posting source code so it is more readable. Click "Go Advanced" and then the "#" icon to insert the tags. Paste your code between them.

Similar Threads

  1. GUI and non-GUI thread problem
    By dimaz in forum Qt Programming
    Replies: 3
    Last Post: 18th September 2008, 21:25
  2. KDE/QWT doubt on debian sarge
    By hildebrand in forum KDE Forum
    Replies: 13
    Last Post: 25th April 2007, 06:13
  3. Replies: 10
    Last Post: 20th March 2007, 22:19
  4. Problem closing a QMainWindow in Qt4.2
    By ian in forum Qt Programming
    Replies: 11
    Last Post: 17th October 2006, 00:49
  5. [QT4] QThread and printing a QList<QPixmap>
    By KShots in forum Qt Programming
    Replies: 3
    Last Post: 24th April 2006, 21:44

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.