Search code examples
c++qttimermqttqtimer

The most accurate timer qt C++


I use QTimer to send periodically 'Ping' packet to the server (MQTT client). But it timer is not absolutely accurate. After some time of working it has some delay and server broken connection. I try to use different Qt::TimerType, but it does not help. I need the most accurate timer. Do you have any ideas?

Thank you!


EDIT (Frederik solution)


I have done something this:

tthread.h

class TThread : public QThread
{
    Q_OBJECT

    void run();

public:
    explicit TThread(QObject *parent = 0);

signals:

private slots:
    void timerOut();
};

tthread.cpp

TThread::TThread(QObject *parent) : QThread(parent)
{

}

void TThread::run()
{
    QTimer timer;
    connect(&timer, SIGNAL(timeout()), this, SLOT(timerOut()), Qt::DirectConnection);
    timer.start(1000);
    timer.moveToThread(this);
    exec();
}

void TThread::timerOut()
{
    QTime time = QTime();
    qDebug() << time.currentTime().toString();
}

main.cpp

TThread thread;
thread.start();
thread.setPriority(QThread::HighPriority);

Solution

  • Run a QTimer on a separate QThread which doesn't do anything else. If you're running the timer on an already busy thread the timeout events may not come on time or not at all.

    Make a worker class e.g. "PingPacketWorker", implement a slot that does pinging. Make a QThread. Connect your QTimer and PingPacketWorker signal/slots. Start the timer. Call moveToThread on the PingPacketWorker and the QTimer, the timer should restart because of moveToThread, note you can only start / stop it on the owner thread!

    You could also increase your QThread's priority since you asked for "the most accurate" solution ...

    Update:

    Also, set QTimer::setTimerType(Qt::PreciseTimer)

    The default Qt::CoarseTimer is less precise (5% of the interval)