QChronoTimer Class
The QChronoTimer class provides repetitive and single-shot timers with intervals given as std::chrono durations. More…
| Header | #include <QChronoTimer> |
|---|---|
| CMake | target_link_libraries(app PRIVATE Nt::Core) |
| Module | Nt Core C++ Classes |
| Since | 6.8 |
| Inherits | QObject |
List of all members, including inherited members
Public Functions
| QChronoTimer(QObject *parent = nullptr) | |
| QChronoTimer(std::chrono::nanoseconds nsec, QObject *parent = nullptr) | |
| ~QChronoTimer() | |
| void | setInterval(std::chrono::nanoseconds nsec) |
| std::chrono::nanoseconds | interval() const |
| std::chrono::nanoseconds | remainingTime() const |
| void | setSingleShot(bool singleShot) |
| bool | isSingleShot() const |
| void | setTimerType(Qt::TimerType atype) |
| Qt::TimerType | timerType() const |
| bool | isActive() const |
| int | id() const |
Public Slots
Signals
| void | timeout() |
Reimplemented Protected Functions
| void | timerEvent(QTimerEvent *event) |
Detailed Description
A QChronoTimer is like QTimer, but its interval is a std::chrono::nanoseconds duration:
using namespace std::chrono_literals;
QChronoTimer timer(250ms);
QObject::connect(&timer, &QChronoTimer::timeout, [] { poll(); });
timer.start();
See also QTimer.
Differences from Qt
The event dispatcher of Nt works in milliseconds, so the interval is rounded up to whole milliseconds when the timer runs, and the timer type is recorded but does not change the precision. There is no singleShot() static function and no bindable properties.
Member Function Documentation
QChronoTimer::QChronoTimer(QObject *parent = nullptr)
Constructs a timer with the given parent and an interval of 0. The timer is not started.
QChronoTimer::QChronoTimer(std::chrono::nanoseconds nsec, QObject *parent = nullptr)
Constructs a timer with the interval nsec and the given parent. The timer is not started.
QChronoTimer::~QChronoTimer()
Destroys the timer, stopping it if it is running.
int QChronoTimer::id() const
Returns the ID of the running timer, or 0 if it is not running.
std::chrono::nanoseconds QChronoTimer::interval() const
Returns the interval of the timer.
See also setInterval().
bool QChronoTimer::isActive() const
Returns true if the timer is running; otherwise returns false.
bool QChronoTimer::isSingleShot() const
Returns true if the timer times out only once; otherwise returns false.
std::chrono::nanoseconds QChronoTimer::remainingTime() const
Returns the time left until the timer times out, zero if it is overdue, or -1 nanosecond if it is not running.
void QChronoTimer::setInterval(std::chrono::nanoseconds nsec)
Sets the interval of the timer to nsec. If the timer is running, it is restarted with the new interval.
See also interval().
void QChronoTimer::setSingleShot(bool singleShot)
Sets whether the timer times out only once to singleShot.
See also isSingleShot().
void QChronoTimer::setTimerType(Qt::TimerType atype)
Sets the timer type to atype. It is recorded but does not change the precision in Nt.
void QChronoTimer::start()
Starts or restarts the timer with its interval.
See also stop() and timeout().
void QChronoTimer::stop()
Stops the timer.
See also start().
void QChronoTimer::timeout()
This signal is emitted when the timer times out.
See also interval() and start().
void QChronoTimer::timerEvent(QTimerEvent *event)
Reimplements QObject::timerEvent().
Qt::TimerType QChronoTimer::timerType() const
Returns the timer type.