[SOLVED] How to make a QTimer as Idle timer in qt using CPP


I am new to Qt programming, I want to make the timer an idle timer.
The question is whether just setting the timer->start(0) will make the timer an idle timer?
How can I know it’s an idle timer.


I’d like to repeat the concerns of @Jeremy Friesner:

On a general-purpose/multitasking OS (such as Qt usually runs under) you want to minimize your app’s CPU usage so that any other programs that may be running can use those leftover CPU cycles (or in the case of a battery-powered device, so that the battery can be conserved). An app that is constantly using CPU cycles while idle would be considered buggy.

Snapshot of Task Manager (while testQTimer0.exe running)

An application with timeout 0 occupies (more than) one core permanently and causes a high currency consumption.

Here we go: a QTimer with interval 0:

#include <QtWidgets>

// main application

int main(int argc, char **argv)
  qDebug() << "Qt Version:" << QT_VERSION_STR;
  QApplication app(argc, argv);
  // setup GUI
  QWidget qWinMain;
  qWinMain.setWindowTitle("QTimer - timeout 0");
  QVBoxLayout qVBox;
  QLabel qLblText("Attention!\nThis may cause ventilation noise.");
  QLabel qLblI;
  QTimer qTimer;
  ushort i = 0;
  // install signal handlers
  QObject::connect(&qTimer, &QTimer::timeout,
    [&]() {
      if (i) qLblI.setText(QString("i: %1").arg(i));
      else app.quit();
  // runtime loop
  return app.exec();


Snapshot of testQTimer0 (GIF Animation)

The interval 0 makes the timer immediately due. This means that a timeout event is appended to the event queue.

Thus, I expect the queue permanently filled with timeout and paint events (for update of qLblI) until the application exits.

I once used such an idle event in the past (before I started to use Qt). I intended to combine the UI event loop with polling "events" from a different source (and lacking any idea about a better alternative). Thereby, the polling function provided itself a time-out option. Thus, the call of the poll function suspended itself the process until either an event came in or the timeout was reached. The most intricate thing was to achieve somehow a load balancing as I tried to distribute the available time equally between the UI and the processing of the other event source. (This was important for the situation where UI events were approaching in high frequency concurrently with the other event source.)

However, I doubt that such a fiddling would be still necessary in Qt. For such a concurrency, there are better options e.g. to run a blocking function in a separate thread.

Answered By – Scheff's Cat

Answer Checked By – Candace Johnson (BugsFixing Volunteer)

Leave a Reply

Your email address will not be published. Required fields are marked *