Page 1 of 2 12 LastLast
Results 1 to 20 of 33

Thread: QObject::killTimers() warning after timer stopped from its own thread

  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 QObject::killTimers() warning after timer stopped from its own thread

    I have seen this topic in many places, on this and another forum, but I think not in the way that a QTimer is being used here.
    I have a timer running in a thread created by moving an object to an instance of QThread (not the old method of sub-classing QThread). Everything appears to run normal except the message in the debugger window: QObject::killTimer: timers cannot be stopped from another thread.

    Relevant code follows:

    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. virtual ~Worker();
    14.  
    15. signals:
    16. void finished();
    17. void updateProgress(int);
    18.  
    19. public slots:
    20. void start();
    21. void stop();
    22. void doWork();
    23.  
    24. private:
    25. QTimer* m_workerTimer;
    26. };
    27.  
    28. class MThread : public QThread
    29. {
    30. Q_OBJECT
    31. public:
    32. MThread(QObject *parent) : QThread(parent) {}
    33.  
    34. static void sleep(unsigned long secs)
    35. {
    36. QThread::sleep(secs);
    37. }
    38.  
    39. static void msleep(unsigned long msecs)
    40. {
    41. QThread::msleep(msecs);
    42. }
    43. };
    44.  
    45. #endif // WORKER_H
    To copy to clipboard, switch view to plain text mode 

    Worker.cpp

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

    FlashWizard.cpp

    Qt Code:
    1. #include "flashwizard.h"
    2. #include "ui_flashwizard.h"
    3. #include "worker.h"
    4.  
    5. #include <QThread>
    6. #include <QDebug>
    7. #include <QMessageBox>
    8.  
    9. FlashWizard::FlashWizard(QWidget *parent) : QWizard(parent), ui(new Ui::FlashWizard)
    10. {
    11. ui->setupUi(this);
    12.  
    13. ui->pbReading->reset();
    14. ui->pbWriting->reset();
    15.  
    16. ui->lblReadingStatus->setText("");
    17. ui->lblReadingStatus->setText("");
    18. ui->lblProgStatus->setText("");
    19.  
    20. connect(ui->introPage, SIGNAL(p1_updateLabelOne(const QString&)), this, SLOT(updateLabel(const QString&)));
    21. connect(this, SIGNAL(currentIdChanged(int)), this, SLOT(pageCleanUp(int)));
    22.  
    23.  
    24. worker = new Worker;
    25. workerThread = new QThread;
    26.  
    27. worker->moveToThread(workerThread);
    28.  
    29. connect(workerThread, SIGNAL(started()), worker, SLOT(start()));
    30. connect(workerThread, SIGNAL(finished()), worker, SLOT(stop()));
    31. connect(worker, SIGNAL(finished()), workerThread, SLOT(quit()));
    32. connect(worker, SIGNAL(finished()), ui->programPage, SLOT(setDone()));
    33. connect(worker, SIGNAL(updateProgress(int)), this, SLOT(updateWritingProgressBar(int)));
    34. }
    35.  
    36. FlashWizard::~FlashWizard()
    37. {
    38. delete ui;
    39. }
    40.  
    41. void FlashWizard::updateWritingProgressBar(const int value)
    42. {
    43. if (ui->pbWriting->maximum() == value)
    44. ui->pbWriting->hide();
    45. ui->pbWriting->setValue(value);
    46. }
    47.  
    48. void FlashWizard::updateLabel(const QString &value)
    49. {
    50. ui->lblReadingStatus->setText(value);
    51. }
    52.  
    53. void FlashWizard::pageCleanUp(int id)
    54. {
    55. if (id == Page_Program)
    56. {
    57. ui->lblProgStatus->setText("Updating ****");
    58. workerThread->start();
    59. qDebug() << "Now using thread: " << worker->thread();
    60. }
    61.  
    62. ui->pbReading->setVisible(false);
    63. ui->lblUpdating->setText("");
    64. }
    65.  
    66.  
    67. void FlashWizard::reject()
    68. {
    69. if (workerThread->isRunning())
    70. {
    71. worker->stop();
    72. }
    73.  
    74. }
    To copy to clipboard, switch view to plain text mode 

    If I click the Cancel button on the Wizard, the loop stops and the thread with it. I get the message at the iteration of where it stops as to the timer being stopped from another thread. Is the context of the main UI thread attempting to stop the timer? It doesn't appear so.
    Last edited by astodolski; 14th December 2012 at 20:24.

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

    Default Re: QObject::killTimers() warning after timer stopped from its own thread

    What does this show?

    Qt Code:
    1. FlashWizard::FlashWizard(...)
    2. {
    3. ...
    4. worker = new Worker;
    5.  
    6. qDebug() << worker->thread();
    7.  
    8. workerThread = new QThread;
    9. worker->moveToThread(workerThread);
    10.  
    11. qDebug() << worker->thread();
    12. qDebug() << worker->m_workerTimer->thread();
    13. }
    To copy to clipboard, switch view to plain text mode 
    Last edited by amleto; 15th December 2012 at 01:56.
    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
    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: QObject::killTimers() warning after timer stopped from its own thread

    I'm not sure but a check from a contributor who posted this suggests that perhaps it's due to creating the timer on the heap in the constructor for the worker object. If so, I got to figure out how to have a task (in this case just a do nothing loop) or several tasks that can be initiated by a timer tick and can that timer live in the worker thread.


    Added after 28 minutes:


    Quote Originally Posted by amleto View Post
    what does this show?

    Qt Code:
    1. FlashWizard::FlashWizard(...)
    2. {
    3. ...
    4. worker = new Worker;
    5.  
    6. qDebug() << worker->thread();
    7.  
    8. workerThread = new QThread;
    9. worker->moveToThread(workerThread);
    10.  
    11. qDebug() << worker->thread();
    12. qDebug() << worker->m_workerTimer->thread();
    13.  
    14. ...
    15. }
    To copy to clipboard, switch view to plain text mode 
    Shows a build error:

    flashwizard.cpp:30: error: C2248: 'Worker::m_workerTimer' : cannot access private member declared in class 'Worker'

    I changed the timer to public, modified some qDebug messages and got the following:

    Main thread: QThread(0x134ae48)
    Worker Object Thread Id: QThread(0x124bbb0)
    Timer object lives in Thread Id: QThread(0x124bbb0)

    So it appears that the timer lives in the worker thread.
    Last edited by astodolski; 15th December 2012 at 01:25.

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

    Default Re: QObject::killTimers() warning after timer stopped from its own thread

    ok, next try. Does this give same thread timer warning?

    Qt Code:
    1. FlashWizard::FlashWizard(...)
    2. {
    3. ...
    4. worker = new Worker;
    5.  
    6. qDebug() << worker->thread();
    7.  
    8. workerThread = new QThread;
    9. worker->moveToThread(workerThread);
    10.  
    11. qDebug() << worker->thread();
    12. qDebug() << worker->m_workerTimer->thread();
    13.  
    14. //connect(workerThread, SIGNAL(started()), worker, SLOT(start()));
    15. connect(workerThread, SIGNAL(finished()), worker, SLOT(stop()));
    16. connect(worker, SIGNAL(finished()), workerThread, SLOT(quit()));
    17. connect(worker, SIGNAL(finished()), ui->programPage, SLOT(setDone()));
    18. connect(worker, SIGNAL(updateProgress(int)), this, SLOT(updateWritingProgressBar(int)));
    19.  
    20. workerThread->start();
    21. QMetaObject::invokeMethod(worker, "start");
    22. QMetaObject::invokeMethod(worker, "stop");
    23. }
    To copy to clipboard, switch view to plain text mode 
    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.

  5. #5
    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: QObject::killTimers() warning after timer stopped from its own thread

    Quote Originally Posted by amleto View Post
    ok, next try. Does this give same thread timer warning?

    Qt Code:
    1. FlashWizard::FlashWizard(...)
    2. {
    3. ...
    4. worker = new Worker;
    5.  
    6. qDebug() << worker->thread();
    7.  
    8. workerThread = new QThread;
    9. worker->moveToThread(workerThread);
    10.  
    11. qDebug() << worker->thread();
    12. qDebug() << worker->m_workerTimer->thread();
    13.  
    14. //connect(workerThread, SIGNAL(started()), worker, SLOT(start()));
    15. connect(workerThread, SIGNAL(finished()), worker, SLOT(stop()));
    16. connect(worker, SIGNAL(finished()), workerThread, SLOT(quit()));
    17. connect(worker, SIGNAL(finished()), ui->programPage, SLOT(setDone()));
    18. connect(worker, SIGNAL(updateProgress(int)), this, SLOT(updateWritingProgressBar(int)));
    19.  
    20. workerThread->start();
    21. QMetaObject::invokeMethod(worker, "start");
    22. QMetaObject::invokeMethod(worker, "stop");
    23. }
    To copy to clipboard, switch view to plain text mode 
    Yes the same message. For what it's worth, I want to note that I get the warning both calling start in the constructor or calling start (where it originally resides) in the slot that's called for the page that displays the progress bar. I just wanted to see if there was a difference - there isn't.

    I took out calling new on the timer in the constructor for the worker. So I now use a reference for the timer in the connect call in the rather than a pointer in start method in the worker:

    Qt Code:
    1. void Worker::start()
    2. {
    3. connect(&m_workerTimer, SIGNAL(timeout()), this, SLOT(doWork()));
    4. m_workerTimer.start(100);
    5. doWork();
    6. }
    To copy to clipboard, switch view to plain text mode 

    Unexpectedly, I don't see the warning now until the worker completes - not when I click the cancel button, which was the previous case.

    Qt Code:
    1. void FlashWizard::reject()
    2. {
    3. if (workerThread->isRunning())
    4. {
    5. worker->stop();
    6. }
    7. }
    To copy to clipboard, switch view to plain text mode 
    Last edited by astodolski; 15th December 2012 at 14:35. Reason: updated contents

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

    Default Re: QObject::killTimers() warning after timer stopped from its own thread

    seems like a Qt bug to me. I think the next step is to breakpoint the warning and debug the qt source.
    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.

  7. #7
    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: QObject::killTimers() warning after timer stopped from its own thread

    When are you calling Worker::stop()? Is the worker thread still running then? Are you always calling Worker::stop() to stop the timer or can it happen that the timer is stopped when the thread object is being destroyed?
    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.


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

    Default Re: QObject::killTimers() warning after timer stopped from its own thread

    Quote Originally Posted by astodolski View Post
    I took out calling new on the timer in the constructor for the worker. So I now use a reference for the timer in the connect call in the rather than a pointer in start method in the worker:

    Qt Code:
    1. void Worker::start()
    2. {
    3. connect(&m_workerTimer, SIGNAL(timeout()), this, SLOT(doWork()));
    4. m_workerTimer.start(100);
    5. doWork();
    6. }
    To copy to clipboard, switch view to plain text mode 

    Unexpectedly, I don't see the warning now until the worker completes - not when I click the cancel button, which was the previous case.

    Qt Code:
    1. void FlashWizard::reject()
    2. {
    3. if (workerThread->isRunning())
    4. {
    5. worker->stop();
    6. }
    7. }
    To copy to clipboard, switch view to plain text mode 
    addressing your edit:
    That is not a reference. It looks like you have changed the timer to be a QTimer member variable instead of a QTimer*. If you are going to change code, please show all the changes otherwise we have to guess what you have done... Specifically, maybe you are passing a parent argument, or maybe you are not. This will affect how objects are moved between threads!

    Qt Code:
    1. &m_workerTimer
    To copy to clipboard, switch view to plain text mode 
    That is not taking or using or making a reference. That is the 'address of' operator you are using there.


    Qt Code:
    1. void FlashWizard::reject()
    2. {
    3. if (workerThread->isRunning())
    4. {
    5. worker->stop();
    6. }
    7. }
    To copy to clipboard, switch view to plain text mode 
    This is wrong and will call the timer stop method from the wrong thread (assuming the timer has been correctly moved to workerThread).
    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.

  9. #9
    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: QObject::killTimers() warning after timer stopped from its own thread

    Ok. I made the illustration (with code) as to the point I was making - less the semantics. The change in use of the timer changes the outcome. I did this change from referencing the article which points to proper use of threads. The other prior suggestions didn't affect the outcome of the message warning.

  10. #10
    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: QObject::killTimers() warning after timer stopped from its own thread

    Quote Originally Posted by astodolski View Post
    Ok. I made the illustration (with code) as to the point I was making - less the semantics. The change in use of the timer changes the outcome. I did this change from referencing the article which points to proper use of threads. The other prior suggestions didn't affect the outcome of the message warning.
    Hmmm.... whaaaat?
    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.


  11. #11
    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: QObject::killTimers() warning after timer stopped from its own thread

    Quote Originally Posted by wysota View Post
    Hmmm.... whaaaat?
    Please read the thread to help with confusion. I don't understand the quip. I was replying to someone else's comment.

  12. #12
    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: QObject::killTimers() warning after timer stopped from its own thread

    I had read the thread before posting. It didn't help with my confusion

    So is your problem fixed or not?
    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.


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

    Default Re: QObject::killTimers() warning after timer stopped from its own thread

    Quote Originally Posted by astodolski View Post
    Ok. I made the illustration (with code) as to the point I was making - less the semantics. The change in use of the timer changes the outcome. I did this change from referencing the article which points to proper use of threads. The other prior suggestions didn't affect the outcome of the message warning.
    Unfortunately your semantics (talking about references) are plain wrong and need to be corrected if you want to continue talking in c++ parlance.

    re bolded sentence: Well it will, or it wont, or it might. It all depends on whether you correctly pass parent pointer to the qtimer and whether there is truly a Qt bug here - you still haven't clarified your change (with code or without) - less or including semantics.
    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.

  14. #14
    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: QObject::killTimers() warning after timer stopped from its own thread

    Guys, but where do you see a Qt bug here?
    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.


  15. #15
    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: QObject::killTimers() warning after timer stopped from its own thread

    Quote Originally Posted by wysota View Post
    Guys, but where do you see a Qt bug here?
    This comment looks like the right question. I don't know if it's a bug. I was working on the suggestions of things to try. I then added a change on my own. This then devolved into trivial banter of what to call a parameter passed to connect. I just want to get the issue resolved as there seems to be lots of discussion on threading in general. I posted the change in the call to connect with a code snippet. It shows a different result. Is it a bug? I don't know. Did I sin in misquoting c++ scripture? Perhaps. All who have read this knows what was intended and I'm just trying to understand. The code posted in total I hope is enough (short of a complete app) to answer the relevant question as to whether the thread was moved, and/or whether the parent pointer was passed to the timer. This is shown at top code listing. Whether wrong or a bug IS the discussion.

    przejrzystość

  16. #16
    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: QObject::killTimers() warning after timer stopped from its own thread

    In my opinion there is no bug here. If I'm right then sometimes you are not stopping the timer before the thread ends. Thus when at some point the object is destructed from within the main thread, the destructor of the timer calls stop() and since the object has a different thread affinity, you get the warning message. You either have to move the worker object back to the main thread before the worker thread ends or you have to make sure the timer is stopped before the thread ends. You'd need some "aboutToFinish" signal that you can use to stop the timer.

    For instance this line of your code is surely invalid:

    Qt Code:
    1. connect(workerThread, SIGNAL(finished()), worker, SLOT(stop()));
    To copy to clipboard, switch view to plain text mode 

    workerThread and worker live in different threads (wT in main thread, w in worker thread) thus the connection is going to be queued and effectively stop() will likely never be called because it'd require workerThread's event loop to be running (which it isn't because the thread has just finished() (after exiting QThread::run()).

    I suggest to sit down with a piece of paper, a pencil and draw some kind of graph or list of states and transitions that need to happen during the lifetime of the thread and the worker. Pay special attention to thread affinity so that you are 100% sure when a particular slot is going to be called. Then implement your state machine. It might be easiest to just push the object back to the original thread before the worker thread dies, e.g. by doing the following:

    Qt Code:
    1. void MyThread::run() {
    2. exec();
    3. if(worker)
    4. worker->moveToThread(QCoreApplication::instance()->thread());
    5. }
    To copy to clipboard, switch view to plain text mode 

    Another possibility would be to connect to the finished() signal with Qt::DirectConnection to do some cleanup before the thread dies assuming that the signal is emitted in the context of the worker thread. If not, subclass QThread and make run() emit some signal (e.g. aboutToFinish()) before it returns.
    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.


  17. #17
    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: QObject::killTimers() warning after timer stopped from its own thread

    Quote Originally Posted by wysota View Post
    In my opinion there is no bug here. If I'm right then sometimes you are not stopping the timer before the thread ends.
    That was related to a previous question I WAS going to attempt to answer

    Thus when at some point the object is destructed from within the main thread, the destructor of the timer calls stop() and since the object has a different thread affinity, you get the warning message. You either have to move the worker object back to the main thread before the worker thread ends or you have to make sure the timer is stopped before the thread ends. You'd need some "aboutToFinish" signal that you can use to stop the timer.
    aboutToFinish would be more convenient.


    It might be easiest to just push the object back to the original thread before the worker thread dies, e.g. by doing the following:

    Qt Code:
    1. void MyThread::run() {
    2. exec();
    3. if(worker)
    4. worker->moveToThread(QCoreApplication::instance()->thread());
    5. }
    To copy to clipboard, switch view to plain text mode 
    This approach then suggests the use of subclassing QThread doesn't it?

    Another possibility would be to connect to the finished() signal with Qt:irectConnection to do some cleanup before the thread dies assuming that the signal is emitted in the context of the worker thread. If not, subclass QThread and make run() emit some signal (e.g. aboutToFinish()) before it returns.
    Another reason to continue use of subclass of QThread.
    Thank you.

  18. #18
    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: QObject::killTimers() warning after timer stopped from its own thread

    Quote Originally Posted by astodolski View Post
    This approach then suggests the use of subclassing QThread doesn't it?
    Yes. There is nothing wrong with subclassing QThread. It gets to be wrong when you start adding slots to the subclass

    However you don't have to subclass QThread at all. You can emit the signal from the Worker object as well when it's done its work or just immediately move it to the main thread. It breaks some OOP concepts but it's still a valid approach.
    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.


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

    Default Re: QObject::killTimers() warning after timer stopped from its own thread

    Quote Originally Posted by amleto View Post
    ok, next try. Does this give same thread timer warning?

    Qt Code:
    1. FlashWizard::FlashWizard(...)
    2. {
    3. ...
    4. worker = new Worker;
    5.  
    6. qDebug() << worker->thread();
    7.  
    8. workerThread = new QThread;
    9. worker->moveToThread(workerThread);
    10.  
    11. qDebug() << worker->thread();
    12. qDebug() << worker->m_workerTimer->thread();
    13.  
    14. //connect(workerThread, SIGNAL(started()), worker, SLOT(start()));
    15. connect(workerThread, SIGNAL(finished()), worker, SLOT(stop()));
    16. connect(worker, SIGNAL(finished()), workerThread, SLOT(quit()));
    17. connect(worker, SIGNAL(finished()), ui->programPage, SLOT(setDone()));
    18. connect(worker, SIGNAL(updateProgress(int)), this, SLOT(updateWritingProgressBar(int)));
    19.  
    20. workerThread->start();
    21. QMetaObject::invokeMethod(worker, "start");
    22. QMetaObject::invokeMethod(worker, "stop");
    23. }
    To copy to clipboard, switch view to plain text mode 
    Quote Originally Posted by astodolski View Post
    Yes the same message. <SNIP>
    Quote Originally Posted by wysota View Post
    Guys, but where do you see a Qt bug here?
    That is a bug if that truly happened. But it seems the OP is not being honest/accurate.
    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.

  20. #20
    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: QObject::killTimers() warning after timer stopped from its own thread

    This works fine for me on Qt 4.8 and 5.0 RC2. I'm not getting any warnings.

    Qt Code:
    1. include <QtCore>
    2.  
    3. class Worker : public QObject {
    4. Q_OBJECT
    5. public:
    6. Worker() : QObject(){
    7. timer = new QTimer(this);
    8. timer->setInterval(1000);
    9. }
    10. public slots:
    11. void start() { qDebug() << Q_FUNC_INFO; timer->start(); }
    12. void stop() { qDebug() << Q_FUNC_INFO; timer->stop(); }
    13. private:
    14. QTimer *timer;
    15. };
    16.  
    17. #include "main.moc"
    18.  
    19. int main(int argc, char **argv) {
    20. QCoreApplication app(argc, argv);
    21. QThread thread;
    22. Worker *worker = new Worker;
    23. worker->moveToThread(&thread);
    24. thread.start();
    25. QMetaObject::invokeMethod(worker, "start");
    26. QMetaObject::invokeMethod(worker, "stop");
    27. QObject::connect(&app, SIGNAL(aboutToQuit()), &thread, SLOT(quit()));
    28. QTimer t;
    29. t.start(1000);
    30. QObject::connect(&t, SIGNAL(timeout()), &app, SLOT(quit()));
    31. app.exec();
    32. thread.wait();
    33. }
    To copy to clipboard, switch view to plain text mode 

    You can build on this example to check your actual code (with timers and stuff).

    Edit: I even added the timer stuff to the code, still works as expected.
    Last edited by wysota; 18th December 2012 at 02:03.
    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.


Similar Threads

  1. Replies: 1
    Last Post: 29th July 2010, 05:41
  2. QObject::killTimer Warning Messages
    By mclark in forum Qt Programming
    Replies: 7
    Last Post: 11th September 2008, 20:10
  3. how to enable a timer in a non-gui thread?
    By zeopha in forum Qt Programming
    Replies: 3
    Last Post: 5th August 2008, 09:29
  4. Replies: 2
    Last Post: 24th March 2008, 16:59
  5. QObject::moveToThread warning
    By mnemonic_fx in forum Qt Programming
    Replies: 3
    Last Post: 10th August 2007, 22:11

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.