Results 1 to 8 of 8

Thread: QThread - Using a slot to exit the thread

  1. #1
    Join Date
    Aug 2010
    Posts
    30
    Thanks
    2

    Default QThread - Using a slot to exit the thread

    Hi,

    I want to inform an object when a thread has finished running. However, I cannot get the thread to exit properly. I have the following code:

    Processor.cpp

    Qt Code:
    1. thread = new QThread;
    2. tw = new ThreadWorker;
    3. connect(tw, SIGNAL(updateStatus(QString)), this, SLOT(statusUpdate(QString)));
    4. tw->doSetup(thread, strDic);
    5. tw->moveToThread(thread);
    6. thread->start();
    7.  
    8. while(thread->isRunning())
    9. {
    10. }
    11.  
    12. qDebug() << "Thread Finished";
    To copy to clipboard, switch view to plain text mode 


    ThreadWorker.cpp

    Qt Code:
    1. void ThreadWorker::doSetup(QThread *thread, const string &path)
    2. {
    3. _strPath = path;
    4. connect(thread, SIGNAL(started()), this, SLOT(run()));
    5. connect(this, SIGNAL(finished()), thread, SLOT(quit())); //tried terminate() also
    6. }
    7.  
    8.  
    9. void ThreadWorker::run()
    10. {
    11. DirectorySearch dicSearch;
    12. vector<string> vecFileList = dicSearch.getFileList(_strPath);
    13. emit updateStatus("Directory Fetched");
    14. emit finished();
    15. }
    To copy to clipboard, switch view to plain text mode 

    The quit() slot does not seem to stop the thread (QThread::isFinished never returns true). Can someone guide me in the right direction?

    Thanks in advance

  2. #2
    Join Date
    Jan 2006
    Location
    Napoli, Italy
    Posts
    621
    Thanks
    5
    Thanked 86 Times in 81 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QThread - Using a slot to exit the thread

    Have you called ThreadWorker::exec() ??

    If you don't ThreadWorker doesn't launch eventLoop and so doesn't receive signals
    A camel can go 14 days without drink,
    I can't!!!

  3. #3
    Join Date
    Aug 2010
    Posts
    30
    Thanks
    2

    Default Re: QThread - Using a slot to exit the thread

    The ThreadWorker class doesn't inherit from QThread. So it has no exec() method.

  4. #4
    Join Date
    Jan 2006
    Location
    Napoli, Italy
    Posts
    621
    Thanks
    5
    Thanked 86 Times in 81 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QThread - Using a slot to exit the thread

    Is ThreadWorker derived from QObject?

    can you post ThreadWorker definition (header file)?
    A camel can go 14 days without drink,
    I can't!!!

  5. #5
    Join Date
    Aug 2010
    Posts
    30
    Thanks
    2

    Default Re: QThread - Using a slot to exit the thread

    Qt Code:
    1. class ThreadWorker : public QObject
    2. {
    3. Q_OBJECT
    4.  
    5. public:
    6. ThreadWorker(void);
    7. ~ThreadWorker(void);
    8.  
    9.  
    10. void doSetup(QThread *, const string &);
    11.  
    12. public slots:
    13. void run();
    14.  
    15. signals:
    16. void updateStatus(QString);
    17. void finished();
    18.  
    19. private:
    20. DirectorySearch dicSearch;
    21. string _strPath;
    22. };
    To copy to clipboard, switch view to plain text mode 

  6. #6
    Join Date
    Jan 2006
    Location
    Napoli, Italy
    Posts
    621
    Thanks
    5
    Thanked 86 Times in 81 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QThread - Using a slot to exit the thread

    When you call QObject::moveToThread, you obtain some warning messages in console?

    Do you tried to start the thread before to move ThreadWorker?
    A camel can go 14 days without drink,
    I can't!!!

  7. #7
    Join Date
    Aug 2010
    Posts
    30
    Thanks
    2

    Default Re: QThread - Using a slot to exit the thread

    I think I understand what's happening now.

    The main thread is being kept busy by the
    while(thread->isRunning())
    {
    }

    Therefore, it cannot process any signals being sent by the ThreadWorker.cpp

  8. #8
    Join Date
    Jan 2006
    Location
    Napoli, Italy
    Posts
    621
    Thanks
    5
    Thanked 86 Times in 81 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QThread - Using a slot to exit the thread



    I thought that you omitted some line of code

    Qt Code:
    1. while(thread->isRunning())
    2. {
    3. ...
    4. }
    To copy to clipboard, switch view to plain text mode 

    PS. As general rule isn't correct to wait thread termination in this way.
    Last edited by mcosta; 27th April 2011 at 11:37. Reason: updated contents
    A camel can go 14 days without drink,
    I can't!!!

Similar Threads

  1. QThread Signal Not Received By Main Thread Slot
    By EF2008 in forum Qt Programming
    Replies: 7
    Last Post: 4th June 2010, 08:06
  2. QThread slot executed in GUI thread
    By tnyblom in forum Qt Programming
    Replies: 13
    Last Post: 25th May 2010, 07:49
  3. QThread crashes on exit
    By mhoover in forum Qt Programming
    Replies: 8
    Last Post: 26th August 2009, 20:19
  4. Replies: 1
    Last Post: 11th September 2008, 20:45
  5. QThread exit()/quit() question
    By TheKedge in forum Qt Programming
    Replies: 1
    Last Post: 28th August 2006, 14:38

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.