6.0.0-rc4Source

QElapsedTimer Class

The QElapsedTimer class provides a fast way to calculate elapsed times. More…

Header#include <QElapsedTimer>
CMaketarget_link_libraries(app PRIVATE Nt::Core)
ModuleNt Core C++ Classes

List of all members, including inherited members

Public Types

typedefClock
enumClockType

Public Functions

voidstart()
qint64restart()
voidinvalidate()
boolisValid() const
qint64nsecsElapsed() const
std::chrono::nanosecondsdurationElapsed() const
qint64elapsed() const
boolhasExpired(qint64 timeoutMs) const
qint64msecsSinceReference() const
qint64msecsTo(const QElapsedTimer &o) const
qint64secsTo(const QElapsedTimer &o) const
std::chrono::nanosecondsdurationTo(const QElapsedTimer &o) const

Static Public Members

ClockTypeclockType()
boolisMonotonic()

Detailed Description

Call start() or restart() to begin timing, then elapsed() or nsecsElapsed() to read the time since then. The timer uses a monotonic clock, so it is not affected by changes to the system time.

QElapsedTimer timer;
timer.start();
slowOperation();
qDebug() << "took" << timer.elapsed() << "ms";

See also QDeadlineTimer.

Differences from Qt

The timer is always based on std::chrono::steady_clock, so clockType() always returns MonotonicClock and isMonotonic() always returns true. hasExpired() treats an invalid timer as expired.

Member Type Documentation

typedef QElapsedTimer::Clock

The clock that the timer reads, std::chrono::steady_clock.

enum QElapsedTimer::ClockType

Describes the clock that a timer uses.

ConstantDescription
QElapsedTimer::SystemTimeThe system time. Not used by Nt.
QElapsedTimer::MonotonicClockA monotonic clock. This is the clock that Nt uses.
QElapsedTimer::TickCounterA tick counter. Not used by Nt.
QElapsedTimer::MachAbsoluteTimeThe Mach absolute time. Not used by Nt.
QElapsedTimer::PerformanceCounterA high-resolution performance counter. Not used by Nt.

See also clockType().

Member Function Documentation

[static] ClockType QElapsedTimer::clockType()

Returns the clock type that the timer uses. This is always MonotonicClock.

See also isMonotonic().

std::chrono::nanoseconds QElapsedTimer::durationElapsed() const

Returns a std::chrono::nanoseconds duration with the time since the timer was started, or zero if the timer is invalid.

This function was introduced in 6.6.

See also nsecsElapsed().

std::chrono::nanoseconds QElapsedTimer::durationTo(const QElapsedTimer &o) const

Returns the time from this timer's start time to the start time of o as a std::chrono::nanoseconds duration.

This function was introduced in 6.6.

See also msecsTo().

qint64 QElapsedTimer::elapsed() const

Returns the number of milliseconds since the timer was started, or 0 if the timer is invalid.

See also nsecsElapsed() and hasExpired().

bool QElapsedTimer::hasExpired(qint64 timeoutMs) const

Returns true if more than timeoutMs milliseconds have elapsed since the timer was started, or if the timer is invalid; otherwise returns false.

See also elapsed().

void QElapsedTimer::invalidate()

Marks the timer as invalid.

See also isValid().

[static] bool QElapsedTimer::isMonotonic()

Returns true if the clock is monotonic, which is always the case.

See also clockType().

bool QElapsedTimer::isValid() const

Returns true if start() or restart() has been called and the timer has not been invalidated; otherwise returns false.

See also invalidate().

qint64 QElapsedTimer::msecsSinceReference() const

Returns the start time of the timer as milliseconds since the clock's reference point.

See also msecsTo().

qint64 QElapsedTimer::msecsTo(const QElapsedTimer &o) const

Returns the number of milliseconds from this timer's start time to the start time of o. The result is negative if o was started earlier.

See also msecsSinceReference().

qint64 QElapsedTimer::nsecsElapsed() const

Returns the number of nanoseconds since the timer was started, or 0 if the timer is invalid.

See also elapsed().

qint64 QElapsedTimer::restart()

Restarts the timer and returns the number of milliseconds that had elapsed since it was started.

See also start() and elapsed().

qint64 QElapsedTimer::secsTo(const QElapsedTimer &o) const

Returns the number of seconds from this timer's start time to the start time of o. The result is negative if o was started earlier.

See also msecsTo().

void QElapsedTimer::start()

Starts the timer and makes it valid.

See also restart() and elapsed().