Results 1 to 20 of 33

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

Threaded View

Previous Post Previous Post   Next Post Next Post
  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.

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.