Results 1 to 3 of 3

Thread: QThread confusion, yet another question about quiting thread

  1. #1
    Join Date
    Dec 2008
    Location
    Poland
    Posts
    383
    Thanks
    52
    Thanked 42 Times in 42 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Android

    Default QThread confusion, yet another question about quiting thread

    Hello,
    I wrote a single piece of code that need to be run in the thread, but I have problem quit'ing the QThread itself.

    Qt Code:
    1. void MainWindow::on_tb_prev_Refresh_clicked()
    2. {
    3. vFilePrev = new videoFilePreview();
    4. th = new QThread();
    5. vFilePrev->moveToThread( th );
    6.  
    7. connect( th, SIGNAL(started()), this, SLOT(threadStarted()));
    8. connect( th, SIGNAL(finished()), this, SLOT(threadFinished()));
    9. connect(vFilePrev, SIGNAL(finishedJob()), this, SLOT( vidPreviewFinished()));
    10. connect(vFilePrev, SIGNAL(finishedJob()), th, SLOT( quit()));
    11.  
    12. th->start();
    13.  
    14. qDebug() << "gui thread: " << this->thread();
    15. }
    16.  
    17. void MainWindow::threadStarted()
    18. {
    19. qDebug() << "Start th";
    20. vFilePrev->makePreview( tmp );
    21. }
    22.  
    23. void MainWindow::vidPreviewFinished()
    24. {
    25. qDebug() << "vidPreviewFinished in thread: " << this->thread();
    26.  
    27. disconnect(vFilePrev, SIGNAL(finishedJob()), this, SLOT( vidPreviewFinished()));
    28. delete vFilePrev;
    29. }
    30.  
    31. void MainWindow::threadFinished()
    32. {
    33. disconnect( th, SIGNAL(started()), this, SLOT(threadStarted()));
    34. disconnect( th, SIGNAL(finished()), this, SLOT(threadFinished()));
    35. disconnect( vFilePrev, SIGNAL(finishedJob()), th, SLOT( quit()));
    36.  
    37. delete th;
    38.  
    39. qDebug() << "delete th";
    40. }
    To copy to clipboard, switch view to plain text mode 

    Qt Code:
    1. #include <QObject>
    2. #include <QDebug>
    3. #include <QStringList>
    4.  
    5. class videoFilePreview : public QObject
    6. {
    7. Q_OBJECT
    8. public:
    9. explicit videoFilePreview(QObject *parent = 0);
    10. void makePreview( QStringList );
    11.  
    12. private:
    13.  
    14.  
    15. signals:
    16. void finishedJob();
    17.  
    18. public slots:
    19.  
    20. };
    To copy to clipboard, switch view to plain text mode 

    Qt Code:
    1. videoFilePreview::videoFilePreview(QObject *parent) :
    2. QObject(parent)
    3. {
    4. }
    5.  
    6. void videoFilePreview::makePreview( QStringList fileList )
    7. {
    8. qDebug() << "in the thread: " << this->thread();
    9.  
    10. emit finishedJob();
    11. }
    To copy to clipboard, switch view to plain text mode 
    And the problem with this is that connect( th, SIGNAL(finished()), this, SLOT(threadFinished())); never invoke SLOT(threadFinished());

    Thanks for any suggestions.
    Regards

  2. #2
    Join Date
    Dec 2008
    Location
    Poland
    Posts
    383
    Thanks
    52
    Thanked 42 Times in 42 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Solved

    SOLVED
    Change:
    Qt Code:
    1. connect( th, SIGNAL( started() ), vFilePrev, SLOT( makePreview() ));
    To copy to clipboard, switch view to plain text mode 
    and:
    Qt Code:
    1. public slots:
    2. void makePreview( );
    To copy to clipboard, switch view to plain text mode 
    I would like to know why when I use
    connect( th, SIGNAL(started()), this, SLOT(threadStarted()));
    the function in this SLOT is run but thread don't seams to react properly (is it because the SLOT is in the same Thread as the QThread, with is GUI thread?).
    I saw in the sysinternals that thread is actually created but never quit's, but when I connect actual Thread code to the SIGNA(started()); QThread react properly (at least with this simple example).

  3. #3
    Join Date
    Jul 2009
    Location
    Enschede, Netherlands
    Posts
    462
    Thanked 69 Times in 67 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Solved

    Quote Originally Posted by Talei View Post
    I would like to know why when I use
    connect( th, SIGNAL(started()), this, SLOT(threadStarted()));
    the function in this SLOT is run but thread don't seams to react properly (is it because the SLOT is in the same Thread as the QThread, with is GUI thread?).
    Absolutely. connections can be autodetected, queued or direct. If they're directly connected, the slot is executed in the emitting thread, if they're queued the slot is executed in the thread its class lives in. In this case QThread did emit started(), but QThread lives in the main thread, just like your MainWindow. No reason to perform the threadStarted() function in a different thread there.
    Horse sense is the thing that keeps horses from betting on people. --W.C. Fields

    Ask Smart Questions

  4. The following user says thank you to franz for this useful post:

    Talei (29th July 2010)

Similar Threads

  1. How can I get the thread ID out of QThread
    By Artschi in forum Qt Programming
    Replies: 9
    Last Post: 8th November 2017, 03:27
  2. GUI thread blocked using QThread.
    By summer_of_69 in forum Qt Programming
    Replies: 11
    Last Post: 18th May 2009, 09:43
  3. [QThread] ptrace in thread
    By Macok in forum Qt Programming
    Replies: 4
    Last Post: 8th February 2009, 21:00
  4. Replies: 4
    Last Post: 26th June 2008, 18:41
  5. QThread, QMessageBox, gui thread
    By TheKedge in forum Qt Programming
    Replies: 1
    Last Post: 25th October 2006, 12:23

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.