Results 1 to 13 of 13

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

  1. #1
    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 Trying to resolve sub-classing QThread - or not

    I have done a search regarding the issue of sub-classing QThread or not. There are what looks almost like a theological debate as to the merits of one versus the other. What I am not being able to find is what are the pitfalls of using an object as a sub-class of QThread. I see benefits of sub-classing such as the availability of isRunning, isFinished, sleep, msleep etc. Without using a subclass, those functions are not available. So other than the statement that it essentially is "no longer necessary", is there a more functional reason?

  2. #2
    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

    Don't know why you think a benefit of subclassing is 'isRunning, isFinished, sleep, msleep are still available'. They are of course still available on the associated QThread!

    The benefit of not sub-classing is that people dont get confused about what threads the signals/slots are (executed) in.
    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.

  3. #3
    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

    Do you need those functions in your subclass? The isRunning and isFinished functions are more for the object controlling the thread, not for the thread itself. I haven't encountered a reason to subclass from QThread honestly. As I see it:

    Subclass QThread if you want different thread functionality
    Do not subclass QThread to create a thread

    So subclass to "expand" the thread control, not to create a thread!

  4. #4
    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
    Do you need those functions in your subclass? The isRunning and isFinished functions are more for the object controlling the thread, not for the thread itself.
    I need to determine from a main thread in the UI that a worker thread has completed

  5. #5
    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: Trying to resolve sub-classing QThread - or not

    Quote Originally Posted by astodolski View Post
    I need to determine from a main thread in the UI that a worker thread has completed
    Connect to the QThread::finished() signal.
    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.


  6. #6
    Join Date
    Jul 2012
    Posts
    244
    Thanks
    27
    Thanked 15 Times in 14 Posts
    Qt products
    Qt4
    Platforms
    Windows

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

    this has been bugging me for some time, too.
    How would i create a thread without sub-classing, reimplementing run() and calling it to start the thread?

  7. #7
    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

    How? Like this.
    Qt Code:
    1. QThread* thread = new QThread;
    2. thread->start();
    To copy to clipboard, switch view to plain text mode 
    colour me confused.
    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.

  8. #8
    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: Trying to resolve sub-classing QThread - or not

    Create an instance of QThread, create an instance of QObject subclass that does something practical, start the thread, move the object to the thread using QObject::moveToThread() and start the practical functionality of the object by posting an event to it or calling its slot (via a signal or QMetaObject::invokeMethod()).
    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.


  9. #9
    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.

  10. #10
    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 amleto View Post
    How? Like this.
    Qt Code:
    1. QThread* thread = new QThread;
    2. thread->start();
    To copy to clipboard, switch view to plain text mode 
    colour me confused.
    I am just trying to seek consensus of what is one way of using QThread as a sub-class or the other more preferred method.

  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: Trying to resolve sub-classing QThread - or not

    There is no need to subclass QThread unless you really want to reimplement run().
    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.


  12. #12
    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 

  13. #13
    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
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.