Results 1 to 16 of 16

Thread: new QWidget in separate thread

  1. #1
    Join Date
    Feb 2007
    Location
    Philadelphia, USA
    Posts
    255
    Thanks
    43
    Thanked 21 Times in 21 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default new QWidget in separate thread

    I create a "new QWidget" in a separate thread. When that thread ends, my widget gets deleted. How can I fix this problem?

  2. #2
    Join Date
    Feb 2006
    Location
    Romania
    Posts
    2,744
    Thanks
    8
    Thanked 541 Times in 521 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: new QWidget in separate thread

    Do you actually create a QWidget in another thread? Or was that just a figure of speech?
    From "Threading support in Qt":
    In practice, the impossibility of using GUI classes in other threads than the main thread can easily be worked around by putting time-consuming operations in a separate worker thread and displaying the results on screen in the main thread when the worker thread is finished. This is the approach used for implementing the Mandelbrot and the Blocking Fortune Client example.
    So, creating widgets outside the GUI thread is out of the question.
    What are you trying to do? There must be another solution.

    EDIT:
    I think a more relevant quote is:
    Although QObject is reentrant, the GUI classes, notably QWidget and all its subclasses, are not reentrant. They can only be used from the main thread

  3. #3
    Join Date
    Feb 2007
    Location
    Philadelphia, USA
    Posts
    255
    Thanks
    43
    Thanked 21 Times in 21 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: new QWidget in separate thread

    Quote Originally Posted by marcel View Post
    Do you actually create a QWidget in another thread? Or was that just a figure of speech?
    From "Threading support in Qt":
    So, creating widgets outside the GUI thread is out of the question.
    What are you trying to do? There must be another solution.
    For example, I want to run the following "ChainLink script" in a separate thread:

    Qt Code:
    1. for j=1:10;
    2. addTab(tab,X,'testing '+toString(j));
    3. end;
    To copy to clipboard, switch view to plain text mode 

    ChainLink works fine in one thread, but now I want to execute all scripts in a separate thread, so that the main GUI (console) can continue responding. I am running into trouble because the widgets all disappear when the thread ends.

  4. #4
    Join Date
    Feb 2006
    Location
    Romania
    Posts
    2,744
    Thanks
    8
    Thanked 541 Times in 521 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: new QWidget in separate thread

    Unfortunately, I am not familiar to ChainLink. How do you run that script? I assume thre must be some kind of interpreter involved...

  5. #5
    Join Date
    Feb 2007
    Location
    Philadelphia, USA
    Posts
    255
    Thanks
    43
    Thanked 21 Times in 21 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: new QWidget in separate thread

    Quote Originally Posted by marcel View Post
    Unfortunately, I am not familiar to ChainLink. How do you run that script? I assume thre must be some kind of interpreter involved...
    Yes... I parse the script and create a syntax tree. Then I execute that syntax tree... each function in the tree comes from a plugin library. One of the functions (in a plugin library) looks like this:

    Qt Code:
    1. bool QWidget2(QWidgetPtr &ret) {
    2. ret=new QWidget;
    3. ret->showNormal();
    4. ret->setAttribute(Qt::WA_DeleteOnClose);
    5. return true;
    6. }
    To copy to clipboard, switch view to plain text mode 

    Everything is fine. But, as I said, what if I want to execute the syntax tree in a separate thread... perhaps you have some idea.

  6. #6
    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: new QWidget in separate thread

    Forget about using widgets in worker threads. You'll need to create the widget in the main thread and communicate with it from the other thread using signals and slots or custom events.

  7. #7
    Join Date
    Feb 2006
    Location
    Romania
    Posts
    2,744
    Thanks
    8
    Thanked 541 Times in 521 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: new QWidget in separate thread

    Everything is fine. But, as I said, what if I want to execute the syntax tree in a separate thread... perhaps you have some idea.
    Absolutely not... It seems to me that the syntax tree is by design made to not work in other threads.
    I see it as a widget factory. This is common in projects where widgets are created dynamically. But widget creation should never be designed to require another thread.
    You should keep the syntax tree in the main thread. How much does it take for it to parse a script and build the widgets?
    For example the designer just blocks a bit when loading very complex UI files...

    Regards

  8. #8
    Join Date
    Feb 2006
    Location
    Romania
    Posts
    2,744
    Thanks
    8
    Thanked 541 Times in 521 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: new QWidget in separate thread

    Quote Originally Posted by wysota View Post
    Forget about using widgets in worker threads. You'll need to create the widget in the main thread and communicate with it from the other thread using signals and slots or custom events.
    No, he just wants to create the widgets from another thread, not to communicate with them.

  9. #9
    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: new QWidget in separate thread

    You can always chop a lengthy process into chunks and execute one chunk at a time and process events in between.

  10. #10
    Join Date
    Feb 2006
    Location
    Romania
    Posts
    2,744
    Thanks
    8
    Thanked 541 Times in 521 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: new QWidget in separate thread

    But this means he must modify his ChainLink component, to make it expose smaller steps.

  11. #11
    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: new QWidget in separate thread

    He is the one who wrote ChainLink, so I don't think this will be a problem

  12. #12
    Join Date
    Feb 2007
    Location
    Philadelphia, USA
    Posts
    255
    Thanks
    43
    Thanked 21 Times in 21 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: new QWidget in separate thread

    Thanks for the discussion. I have thought of a solution that I am happy with. Just one more question (see end of post).

    ChainLink scripts consist mostly of non-GUI numerical manipulations (like Matlab programs), so they do deserve to be executed in a work thread. However, it is clear based on your comments that creation of widgets within a separate work thread is out of the question. This is a problem since ChainLink has functions to create plots and graphs. Nevertheless, as pointed out in this thread, creating/manipulating widgets does not take much time (as compared with intense numerical computations). Therefore, a hybrid solution should be possible.

    So here is what I plan to do:

    1. As part of function definition, the user must identify which plugin functions must be executed in the GUI thread.

    2. When the work thread encounters a syntax tree node containing a "GUI function", the thread pauses, and a signal is emitted: pleaseExecuteThisNodeInTheGuiThread().

    3. When the GUI thread receives this signal, he executes the appropriate node, and sends back a signal: theRequestedNodeHasBeenExecuted(bool success).

    4. When the work thread receives this signal, it resumes its work.

    Now, my question is: what is the best way for a thread to pause until a signal is received.

    Thanks!

  13. #13
    Join Date
    Feb 2007
    Location
    Philadelphia, USA
    Posts
    255
    Thanks
    43
    Thanked 21 Times in 21 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: new QWidget in separate thread

    Okay I got it working. I used QWaitCondition, and now it works beautifully.

    Basically, I execute the syntax tree in a separate thread. Whenever I need to execute a GUI function, I send control back to the main GUI thread using a signal/slot connection, and a QWaitCondition for the response. Now I see how a thread can effectively execute a GUI function in the main thread.

  14. #14
    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: new QWidget in separate thread

    Quote Originally Posted by magland View Post
    Now, my question is: what is the best way for a thread to pause until a signal is received.
    There are two nice possibilities. One is to use custom events instead of signals and use sendEvent() instead of postEvent(). This should make the sending thread block until the event is processed. The other solution is to use blocking signal connections. See the description of QObject::connect() and the description of Qt::BlockingQueuedConnection which you should use. Your code will be much cleaner than when using wait conditions.

  15. The following user says thank you to wysota for this useful post:

    magland (7th February 2008)

  16. #15
    Join Date
    Feb 2007
    Location
    Philadelphia, USA
    Posts
    255
    Thanks
    43
    Thanked 21 Times in 21 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: new QWidget in separate thread

    Quote Originally Posted by wysota View Post
    There are two nice possibilities. One is to use custom events instead of signals and use sendEvent() instead of postEvent(). This should make the sending thread block until the event is processed. The other solution is to use blocking signal connections. See the description of QObject::connect() and the description of Qt::BlockingQueuedConnection which you should use. Your code will be much cleaner than when using wait conditions.

    I am now using Qt::BlockingQueuedConnection as you suggest, and indeed the code is cleaner. Thanks!

    There is one thing I am concerned about (although it doesn't seem to be a problem based on some trials). I don't want the thread to resume execution until AFTER the slot has been processed. With Qt::BlockingQueuedConnection, the thread is blocked until the signal is delivered... but is it safe to assume that the slot will be processed before unblocking?

  17. #16
    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: new QWidget in separate thread

    Yes. That's the whole point. At least I think so

Similar Threads

  1. handling paintGL in a separate thread
    By al101 in forum Qt Programming
    Replies: 1
    Last Post: 15th May 2007, 18:04
  2. KDE/QWT doubt on debian sarge
    By hildebrand in forum KDE Forum
    Replies: 13
    Last Post: 25th April 2007, 07:13
  3. Replies: 10
    Last Post: 20th March 2007, 23:19
  4. Problem closing a QMainWindow in Qt4.2
    By ian in forum Qt Programming
    Replies: 11
    Last Post: 17th October 2006, 01:49
  5. [QT4] QThread and printing a QList<QPixmap>
    By KShots in forum Qt Programming
    Replies: 3
    Last Post: 24th April 2006, 22: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.