Results 1 to 13 of 13

Thread: Trying to resolve sub-classing QThread - or not

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    May 2012
    Posts
    37
    Thanks
    5
    Thanked 1 Time in 1 Post
    Qt products
    Qt4 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Trying to resolve sub-classing QThread - or not

    Check the updated documentation: http://qt-project.org/doc/qt-4.8/QThread.html . Basically:

    Qt Code:
    1. class Worker : public QObject
    2. {
    3. Q_OBJECT
    4.  
    5. public slots:
    6. void doWork() {
    7. ...
    8. }
    9. };
    10.  
    11. void MyObject::putWorkerInAThread()
    12. {
    13. Worker *worker = new Worker;
    14. QThread *workerThread = new QThread(this);
    15.  
    16. connect(workerThread, SIGNAL(started()), worker, SLOT(doWork()));
    17. connect(workerThread, SIGNAL(finished()), worker, SLOT(deleteLater()));
    18. worker->moveToThread(workerThread);
    19.  
    20. // Starts an event loop, and emits workerThread->started()
    21. workerThread->start();
    22. }
    To copy to clipboard, switch view to plain text mode 

    You can also connect the thread finished() signal to its own deleteLater slot.

    Quote Originally Posted by astodolski View Post
    I need to determine from a main thread in the UI that a worker thread has completed
    You don't need to subclass for that. Either use the signal or check the isRunning from the main thread.

  2. #2
    Join Date
    Oct 2012
    Location
    The land of pain (NY)
    Posts
    99
    Thanks
    7
    Thanked 3 Times in 2 Posts
    Qt products
    Qt4 Qt/Embedded
    Platforms
    MacOS X Windows Android

    Default Re: Trying to resolve sub-classing QThread - or not

    Quote Originally Posted by Kwakkie View Post

    You don't need to subclass for that. Either use the signal or check the isRunning from the main thread.
    Which signal?

    If checking isRunning, then it will always return true if calling from the man thread. I need to check the status of the worker object moved to the other thread.

    My code's constuctor in FlashWizard.cpp:

    Qt Code:
    1. FlashWizard::FlashWizard(QWidget *parent) : QWizard(parent), ui(new Ui::FlashWizard)
    2. {
    3. ui->setupUi(this);
    4.  
    5. ui->pbReading->reset();
    6. ui->pbWriting->reset();
    7.  
    8. ui->lblReadingStatus->setText("");
    9. ui->lblReadingStatus->setText("");
    10. ui->lblProgStatus->setText("");
    11.  
    12. connect(ui->introPage, SIGNAL(p1_updateLabelOne(const QString&)), this, SLOT(updateLabel(const QString&)));
    13. connect(this, SIGNAL(currentIdChanged(int)), this, SLOT(pageCleanUp(int)));
    14.  
    15. worker = new Worker;
    16. workerThread = new QThread;
    17.  
    18. worker->moveToThread(workerThread);
    19.  
    20. connect(workerThread, SIGNAL(started()), worker, SLOT(start()));
    21. connect(worker, SIGNAL(finished()), workerThread, SLOT(quit()));
    22. connect(worker, SIGNAL(finished()), worker, SLOT(deleteLater()));
    23. connect(workerThread, SIGNAL(finished()), workerThread, SLOT(deleteLater()));
    24. connect(workerThread, SIGNAL(finished()), ui->programPage, SLOT(setDone()));
    25. connect(worker, SIGNAL(updateProgress(int)), this, SLOT(updateWritingProgressBar(int)));
    26. connect(worker, SIGNAL(finished()), ui->programPage, SLOT(setDone()));
    27. connect(workerThread, SIGNAL(finished()), worker, SLOT(stop()), Qt::QueuedConnection);
    28. }
    To copy to clipboard, switch view to plain text mode 

    ..and the worker.h

    Qt Code:
    1. #ifndef WORKER_H
    2. #define WORKER_H
    3.  
    4. #include <QObject>
    5. #include <QTimer>
    6. #include <QThread>
    7.  
    8. class Worker : public QObject
    9. {
    10. Q_OBJECT
    11. public:
    12. explicit Worker(QObject *parent = 0);
    13.  
    14. signals:
    15. void finished();
    16. void updateProgress(int);
    17.  
    18. public slots:
    19. void start();
    20. void stop();
    21. void doWork();
    22.  
    23. private:
    24. QTimer* m_workerTimer;
    25. };
    26.  
    27. class MThread : public QThread
    28. {
    29. Q_OBJECT
    30. public:
    31. MThread(QObject *parent) : QThread(parent) {}
    32.  
    33. static void sleep(unsigned long secs)
    34. {
    35. QThread::sleep(secs);
    36. }
    37.  
    38. static void msleep(unsigned long msecs)
    39. {
    40. QThread::msleep(msecs);
    41. }
    42. };
    43.  
    44. #endif // WORKER_H
    To copy to clipboard, switch view to plain text mode 

    Code

    Qt Code:
    1. #include "worker.h"
    2. #include <QThread>
    3. #include <QDebug>
    4.  
    5. Worker::Worker(QObject *parent) : QObject(parent)
    6. {
    7. m_workerTimer = new QTimer(this);
    8. }
    9.  
    10. void Worker::start()
    11. {
    12. connect(m_workerTimer, SIGNAL(timeout()), this, SLOT(doWork()));
    13. qDebug() << "Object constructor: " << thread();
    14. m_workerTimer->start(100);
    15. }
    16.  
    17. void Worker::stop()
    18. {
    19. m_workerTimer->stop();
    20. emit finished();
    21. }
    22.  
    23. void Worker::doWork()
    24. {
    25. for (int i = 0; i < 1000000; i++)
    26. {
    27. if (i % 10000 == 0)
    28. {
    29. emit updateProgress(i / 10000);
    30. qDebug() << (i / 10000);
    31. static_cast<MThread*>(QThread::currentThread())->msleep(10);
    32. }
    33. }
    34.  
    35. emit updateProgress(100);
    36. emit finished();
    37. }
    To copy to clipboard, switch view to plain text mode 

  3. #3
    Join Date
    Sep 2011
    Posts
    1,241
    Thanks
    3
    Thanked 127 Times in 126 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Trying to resolve sub-classing QThread - or not

    Look, if you want to check workerThread->isFinished, then it isn't very clever to be deleting the workerThread when it is finished, is it?

    If you remove this
    Qt Code:
    1. connect(workerThread, SIGNAL(finished()), workerThread, SLOT(deleteLater()));
    To copy to clipboard, switch view to plain text mode 
    Then you can query workerThread->isFinished at any time you like.
    If you have a problem, CUT and PASTE your code. Do not retype or simplify it. Give a COMPLETE and COMPILABLE example of your problem. Otherwise we are all guessing the problem from a fabrication where relevant details are often missing.

Similar Threads

  1. Replies: 6
    Last Post: 20th July 2012, 22:50
  2. Replies: 1
    Last Post: 11th May 2010, 14:29
  3. Sub classing QLineEdit
    By kavinsiva in forum Newbie
    Replies: 2
    Last Post: 13th October 2009, 17:48
  4. Sub classing QListWidgetItem
    By Majdi in forum Newbie
    Replies: 3
    Last Post: 27th February 2009, 18:30
  5. Replies: 18
    Last Post: 22nd February 2006, 20:51

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
  •  
Qt is a trademark of The Qt Company.