Results 1 to 3 of 3

Thread: QThread: Thread does not leave event loop on quit() using Queued signals, why ?

  1. #1
    Join Date
    Aug 2010
    Posts
    17
    Thanks
    4
    Qt products
    Qt4
    Platforms
    Windows

    Default QThread: Thread does not leave event loop on quit() using Queued signals, why ?

    I have a small worker thread:

    Qt Code:
    1. class ReloadDirectoryThread : public QThread
    2. {
    3. Q_OBJECT
    4. public:
    5. ReloadDirectoryThread()
    6. : QThread() {
    7. };
    8.  
    9. ~ReloadDirectoryThread() {
    10. };
    11.  
    12. void run() {
    13. exec();
    14. };
    15.  
    16. public slots:
    17. void importImage(int row, QString filePath);
    18. signals:
    19. void imageImported(int row, QIcon* icon);
    20. };
    To copy to clipboard, switch view to plain text mode 

    Its task is to open Images pointed to by filePath, convert them into a QIcon and emit each new Icon:

    Qt Code:
    1. void ReloadDirectoryThread::importImage(int row, QString filePath)
    2. {
    3. QIcon* icon = new QIcon(QPixmap(filePath));
    4. emit imageImported(row, icon);
    5. }
    To copy to clipboard, switch view to plain text mode 

    The thread will be started from the main-application thread:

    Qt Code:
    1. { ...
    2. reloadDirectoryThread = new ReloadDirectoryThread;
    3.  
    4. connect(this, SIGNAL(importImage(int,QString)),
    5. reloadDirectoryThread, SLOT(importImage(int,QString)), Qt::QueuedConnection);
    6.  
    7. connect(reloadDirectoryThread, SIGNAL(imageImported(int,QIcon*)),
    8. this, SLOT(imageImported(int,QIcon*)), Qt::QueuedConnection);
    9.  
    10. reloadDirectoryThread->moveToThread(reloadDirectoryThread);
    11. reloadDirectoryThread->start();
    12. ...}
    To copy to clipboard, switch view to plain text mode 


    The Thread should be shut down when closing the app:

    Qt Code:
    1. {...
    2. reloadDirectoryThread->quit();
    3. reloadDirectoryThread->wait(3000);
    4. delete reloadDirectoryThread;
    5. ...}
    To copy to clipboard, switch view to plain text mode 

    This will tell the thread to quit its eventLoop and waits at most 3 seconds for the thread to finish. At this time all "importImage"-signals are already posted into the event queue.

    My problem is that the worker-thread always completes all messages (what in fact lasts more than three seconds) until the Thread Object can be deleted safely.

    Any ideas what's going wrong ? Why does the thread not stop executing events from the eventqueue when calling quit() ?

    .. frank

  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: QThread: Thread does not leave event loop on quit() using Queued signals, why ?

    Most likely the event loop will quit only when all already scheduled events are processed. And since you are sending multiple signals from another thread before you call quit() then all those signals will be handled first and only then the loop will exit.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


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

    franku (1st December 2010)

  4. #3
    Join Date
    Aug 2010
    Posts
    17
    Thanks
    4
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QThread: Thread does not leave event loop on quit() using Queued signals, why ?

    I see. So I will have a flag to omit the work on a shutdown.
    Qt Code:
    1. void ReloadDirectoryThread::importImage(int row, QString filePath)
    2. {
    3. if(!_shutdown) {
    4. QIcon* icon = new QIcon(QPixmap(filePath));
    5. emit imageImported(row, icon);
    6. }
    7. }
    To copy to clipboard, switch view to plain text mode 

    I thought there was a mistake. Thanks.

Similar Threads

  1. Terminate a QThread with an event loop
    By paolom in forum Qt Programming
    Replies: 2
    Last Post: 12th May 2010, 12:53
  2. Application won't quit until event loop quits
    By caelestis in forum Qt Programming
    Replies: 6
    Last Post: 11th February 2010, 08:21
  3. QThread event loop blocking the GUI
    By JoeMerchant in forum Qt Programming
    Replies: 4
    Last Post: 18th July 2009, 08:54
  4. QThread event loop seems blocked
    By eurodatar in forum Qt Programming
    Replies: 3
    Last Post: 6th May 2009, 17:50
  5. Replies: 4
    Last Post: 26th June 2008, 19:41

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.