0

I have a subclass of QObject called Updater that I want to use to manage some widgets in my app. I want it to run updateDisp() every 16 ms, so I created a QTimer in the constructor and connected the timeout signal to the updateDisp() slot. However, updateDisp() never runs, and I can't for the life of me figure out why.

in updater.h:

class Updater : public QObject {
    Q_OBJECT
    ToUpdate* toUpdate;
    QTimer* timer;
    ...
    public slots:
    void updateDisp();
};

in updater.cpp:

Updater::Updater(ToUpdate* t, QObject *parent)
    : QObject(parent) {
    toUpdate = t;
    timer = new QTimer(this);
    connect(timer, SIGNAL(timeout()), this, SLOT(updateDisp()));
    timer->setInterval(16);
    timer->start();}

I instantiate an Updater object in MainWindow.cpp. Also, I have the GUI thread separate from main() (using winapi CreateThread()); I've seen some other posts about problems with QTimers and QThreads but obviously this is a bit different.

TylerKehne
  • 361
  • 2
  • 12

1 Answers1

1

There's some similar issues. I'd a similar problem in the past: https://github.com/codelol/QtTimer/commit/cef130b7ad27c9ab18e03c15710ace942381c82a#commitcomment-10696869

Basically it seems that Qt5 timer doesn't work as expected while in background, it's sync with the animation timer... which doesn't run often while in background.

This guy solved a similar issue setting the timer type to Qt::PreciseTimer https://github.com/codelol/QtTimer/commit/cef130b7ad27c9ab18e03c15710ace942381c82a#commitcomment-10696869

timer->setTimerType(Qt::PreciseTimer);

The description of the timer types: http://doc.qt.io/qt-5/qt.html#TimerType-enum

Qt::PreciseTimer 0 Precise timers try to keep millisecond accuracy

Not sure if they're the exactly same problem, but you can give a try on that.

dfranca
  • 5,156
  • 2
  • 32
  • 60
  • :( It happens only in background, or always? – dfranca May 06 '15 at 16:20
  • Always. The slot function never gets called even once. – TylerKehne May 06 '15 at 16:22
  • How's your thread implementation? Should change to something like this answer? http://stackoverflow.com/questions/22399868/qobjectstarttimer-timers-can-only-be-used-with-threads-started-with-qthread – dfranca May 06 '15 at 16:31
  • I call QtAppication::exec() from the thread I create with CreateThread(), and all my widgets are created there (everything else works fine) so I feel like that can't be the problem. I never create any QThreads. – TylerKehne May 06 '15 at 16:38
  • could you add more information on how you create your thread and where you call exec()? – Hurzelchen May 06 '15 at 18:13