Results 1 to 3 of 3

Thread: Multi Thread with Qt - QThread and SemiModal Dialog

  1. #1
    Join Date
    Jul 2020
    Posts
    11
    Thanks
    4
    Thanked 1 Time in 1 Post
    Qt products
    Qt5

    Default Multi Thread with Qt - QThread and SemiModal Dialog

    Hello !
    I'm stuck with understanding threads.
    First, the prerequisites I want to cope with :
    1. have a window that display an image of my code's evolution
    2. have an event loop running for this window, because I want to be able for instance to change camera via user mouse input
    3. keep my code's main operations in the main thread, because my code has to run both with and without Qt
    4. preferencially keep QApplication in the main thread, because I assume that otherwise I would somehow have to rewrite every other Qt Instance with threads and I don't really have time to achieve that


    So, I want to have a widget (QDialog) that displays things coming from the main thread (1). In the beginning, I had a look at non modal dialog, but the problem is that QDialog->exec() stucks the main program in a processEvent() loop, and QDialog->show() does not process asynchroneous events from user, and need frequent updates that I can't ensure (especially with 3D images, for which moving the camera is a frame by frame action).

    Therefore, I went into thread, but I can't get it to work. I understood that every QWidget has to be in the main thread, but I tried to trigger the static function of QCoreApplication:rocessEvents() from various threads with loops, to call it from inside a function of the Widget, to call it directly from a «worker» QObject (as shown in https://doc.qt.io/qt-5/qthread.html#details) but it does not accept to work this way, in this example, the «most important part of the operation» has to be done in the worker, which does not comply with (4).
    I've also read carefully https://doc.qt.io/qt-5/threads-qobject.html ... but I'm confused.

    Thanks in advance for any idea about how this is supposed to be done !

  2. #2
    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: Multi Thread with Qt - QThread and SemiModal Dialog

    Unfortunately with Qt, QApplication and all GUI-related things must live in the main thread. You can have calculations of things like QImage or other off-screen paint devices occur in a different thread, but they must be displayed by a QWidget in the main thread.

    So, I am afraid you need to invert your design so that your calculations are done in worker threads instead of in the main thread.

    If you want to use a QDialog, then you need to set its flag to be non-modal, and use show() instead of exec() to display it. exec() causes QDialog to start its own internal event loop, which ignores events from outside. show() uses the same event loop as your application.
    <=== 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.

  3. The following user says thank you to d_stranz for this useful post:

    Stophe (24th September 2020)

  4. #3
    Join Date
    Jul 2020
    Posts
    11
    Thanks
    4
    Thanked 1 Time in 1 Post
    Qt products
    Qt5

    Default Re: Multi Thread with Qt - QThread and SemiModal Dialog

    Oh, okay !
    So now I know some ideas I had weren't possible the way I thought them. I'll try to call some functions or to rethink my design, when possible, and when I need such behavior !
    Nowadays, I use exec(), and sometimes show() followed by QCoreApplication:rocessEvent() when it's possible.
    Maybe I'll come to this later on this topic (maybe not, depending on the time I have), thanks for your help !

Similar Threads

  1. Replies: 1
    Last Post: 4th October 2012, 15:49
  2. Replies: 11
    Last Post: 12th September 2012, 17:25
  3. Replies: 1
    Last Post: 4th September 2012, 15:13
  4. Replies: 1
    Last Post: 28th March 2012, 19:58
  5. Replies: 5
    Last Post: 22nd February 2011, 22:21

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.