Skip to content

QTimer

ModuleCore
Include
#include <QCoroTimer>
CMake
target_link_libraries(myapp QCoro::Core)
QTimer timer;
timer.start(1s);
co_await timer;

The QCoro frameworks allows co_awaiting on QTimer object. The co-awaiting coroutine is suspended, until the timer finishes, that is until QTimer::timeout() signal is emitted.

The timer must be active. If the timer is not active (not started yet or already finished) the co_await expression will return immediately.

To make it work, include QCoroTimer in your implementation.

#include <QCoroTimer>
#include <chrono>

using namespace std::chrono_literals;

QCoro::Task<> MyClass::pretendWork() {
    // Creates and starts a QTimer that will tick every second
    QTimer timer;
    timer.setInterval(1s);
    timer.start();

    for (int i = 1; i <= 100; ++i) {
        // Wait for the timer to tick
        co_await timer;
        // Update the progress bar value
        mProgressBar->setValue(i);
        // And repeat...
    }
    // ... until the for loop finishes.
}