0
votes

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.

1
try timer = new QTimer(parent);nkcode

1 Answers

1
votes

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.