Struct qt_core::QElapsedTimer

source ·
#[repr(C)]
pub struct QElapsedTimer { /* private fields */ }
Expand description

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

C++ class: QElapsedTimer.

C++ documentation:

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

The QElapsedTimer class is usually used to quickly calculate how much time has elapsed between two events. Its API is similar to that of QTime, so code that was using that can be ported quickly to the new class.

However, unlike QTime, QElapsedTimer tries to use monotonic clocks if possible. This means it's not possible to convert QElapsedTimer objects to a human-readable time.

The typical use-case for the class is to determine how much time was spent in a slow operation. The simplest example of such a case is for debugging purposes, as in the following example:

QElapsedTimer timer; timer.start();

slowOperation1();

qDebug() << “The slow operation took” << timer.elapsed() << “milliseconds”;

In this example, the timer is started by a call to start() and the elapsed timer is calculated by the elapsed() function.

The time elapsed can also be used to recalculate the time available for another operation, after the first one is complete. This is useful when the execution must complete within a certain time period, but several steps are needed. The waitFor-type functions in QIODevice and its subclasses are good examples of such need. In that case, the code could be as follows:

void executeSlowOperations(int timeout) { QElapsedTimer timer; timer.start(); slowOperation1();

int remainingTime = timeout - timer.elapsed(); if (remainingTime > 0) slowOperation2(remainingTime); }

Another use-case is to execute a certain operation for a specific timeslice. For this, QElapsedTimer provides the hasExpired() convenience function, which can be used to determine if a certain number of milliseconds has already elapsed:

void executeOperationsForTime(int ms) { QElapsedTimer timer; timer.start();

while (!timer.hasExpired(ms)) slowOperation1(); }

It is often more convenient to use QDeadlineTimer in this case, which counts towards a timeout in the future instead of tracking elapsed time.

Implementations§

source§

impl QElapsedTimer

source

pub unsafe fn clock_type() -> ClockType

Returns the clock type that this QElapsedTimer implementation uses.

Calls C++ function: static QElapsedTimer::ClockType QElapsedTimer::clockType().

C++ documentation:

Returns the clock type that this QElapsedTimer implementation uses.

See also isMonotonic().

source

pub unsafe fn copy_from( &self, other: impl CastInto<Ref<QElapsedTimer>> ) -> Ref<QElapsedTimer>

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

Calls C++ function: QElapsedTimer& QElapsedTimer::operator=(const QElapsedTimer& other).

C++ documentation:

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

The QElapsedTimer class is usually used to quickly calculate how much time has elapsed between two events. Its API is similar to that of QTime, so code that was using that can be ported quickly to the new class.

However, unlike QTime, QElapsedTimer tries to use monotonic clocks if possible. This means it's not possible to convert QElapsedTimer objects to a human-readable time.

The typical use-case for the class is to determine how much time was spent in a slow operation. The simplest example of such a case is for debugging purposes, as in the following example:

QElapsedTimer timer; timer.start();

slowOperation1();

qDebug() << “The slow operation took” << timer.elapsed() << “milliseconds”;

In this example, the timer is started by a call to start() and the elapsed timer is calculated by the elapsed() function.

The time elapsed can also be used to recalculate the time available for another operation, after the first one is complete. This is useful when the execution must complete within a certain time period, but several steps are needed. The waitFor-type functions in QIODevice and its subclasses are good examples of such need. In that case, the code could be as follows:

void executeSlowOperations(int timeout) { QElapsedTimer timer; timer.start(); slowOperation1();

int remainingTime = timeout - timer.elapsed(); if (remainingTime > 0) slowOperation2(remainingTime); }

Another use-case is to execute a certain operation for a specific timeslice. For this, QElapsedTimer provides the hasExpired() convenience function, which can be used to determine if a certain number of milliseconds has already elapsed:

void executeOperationsForTime(int ms) { QElapsedTimer timer; timer.start();

while (!timer.hasExpired(ms)) slowOperation1(); }

It is often more convenient to use QDeadlineTimer in this case, which counts towards a timeout in the future instead of tracking elapsed time.

source

pub unsafe fn elapsed(&self) -> i64

Returns the number of milliseconds since this QElapsedTimer was last started.

Calls C++ function: qint64 QElapsedTimer::elapsed() const.

C++ documentation:

Returns the number of milliseconds since this QElapsedTimer was last started.

Calling this function on a QElapsedTimer that is invalid results in undefined behavior.

See also start(), restart(), hasExpired(), isValid(), and invalidate().

source

pub unsafe fn has_expired(&self, timeout: i64) -> bool

Returns true if this QElapsedTimer has already expired by timeout milliseconds (that is, more than timeout milliseconds have elapsed). The value of timeout can be -1 to indicate that this timer does not expire, in which case this function will always return false.

Calls C++ function: bool QElapsedTimer::hasExpired(qint64 timeout) const.

C++ documentation:

Returns true if this QElapsedTimer has already expired by timeout milliseconds (that is, more than timeout milliseconds have elapsed). The value of timeout can be -1 to indicate that this timer does not expire, in which case this function will always return false.

See also elapsed() and QDeadlineTimer.

source

pub unsafe fn invalidate(&self)

Marks this QElapsedTimer object as invalid.

Calls C++ function: void QElapsedTimer::invalidate().

C++ documentation:

Marks this QElapsedTimer object as invalid.

An invalid object can be checked with isValid(). Calculations of timer elapsed since invalid data are undefined and will likely produce bizarre results.

See also isValid(), start(), and restart().

source

pub unsafe fn is_monotonic() -> bool

Returns true if this is a monotonic clock, false otherwise. See the information on the different clock types to understand which ones are monotonic.

Calls C++ function: static bool QElapsedTimer::isMonotonic().

C++ documentation:

Returns true if this is a monotonic clock, false otherwise. See the information on the different clock types to understand which ones are monotonic.

See also clockType() and QElapsedTimer::ClockType.

source

pub unsafe fn is_valid(&self) -> bool

Returns false if the timer has never been started or invalidated by a call to invalidate().

Calls C++ function: bool QElapsedTimer::isValid() const.

C++ documentation:

Returns false if the timer has never been started or invalidated by a call to invalidate().

See also invalidate(), start(), and restart().

source

pub unsafe fn msecs_since_reference(&self) -> i64

Returns the number of milliseconds between last time this QElapsedTimer object was started and its reference clock's start.

Calls C++ function: qint64 QElapsedTimer::msecsSinceReference() const.

C++ documentation:

Returns the number of milliseconds between last time this QElapsedTimer object was started and its reference clock’s start.

This number is usually arbitrary for all clocks except the QElapsedTimer::SystemTime clock. For that clock type, this number is the number of milliseconds since January 1st, 1970 at 0:00 UTC (that is, it is the Unix time expressed in milliseconds).

On Linux, Windows and Apple platforms, this value is usually the time since the system boot, though it usually does not include the time the system has spent in sleep states.

See also clockType() and elapsed().

source

pub unsafe fn msecs_to(&self, other: impl CastInto<Ref<QElapsedTimer>>) -> i64

Returns the number of milliseconds between this QElapsedTimer and other. If other was started before this object, the returned value will be negative. If it was started later, the returned value will be positive.

Calls C++ function: qint64 QElapsedTimer::msecsTo(const QElapsedTimer& other) const.

C++ documentation:

Returns the number of milliseconds between this QElapsedTimer and other. If other was started before this object, the returned value will be negative. If it was started later, the returned value will be positive.

The return value is undefined if this object or other were invalidated.

See also secsTo() and elapsed().

source

pub unsafe fn new() -> CppBox<QElapsedTimer>

Constructs an invalid QElapsedTimer. A timer becomes valid once it has been started.

Calls C++ function: [constructor] void QElapsedTimer::QElapsedTimer().

C++ documentation:

Constructs an invalid QElapsedTimer. A timer becomes valid once it has been started.

This function was introduced in Qt 5.4.

See also isValid() and start().

source

pub unsafe fn new_copy( other: impl CastInto<Ref<QElapsedTimer>> ) -> CppBox<QElapsedTimer>

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

Calls C++ function: [constructor] void QElapsedTimer::QElapsedTimer(const QElapsedTimer& other).

C++ documentation:

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

The QElapsedTimer class is usually used to quickly calculate how much time has elapsed between two events. Its API is similar to that of QTime, so code that was using that can be ported quickly to the new class.

However, unlike QTime, QElapsedTimer tries to use monotonic clocks if possible. This means it's not possible to convert QElapsedTimer objects to a human-readable time.

The typical use-case for the class is to determine how much time was spent in a slow operation. The simplest example of such a case is for debugging purposes, as in the following example:

QElapsedTimer timer; timer.start();

slowOperation1();

qDebug() << “The slow operation took” << timer.elapsed() << “milliseconds”;

In this example, the timer is started by a call to start() and the elapsed timer is calculated by the elapsed() function.

The time elapsed can also be used to recalculate the time available for another operation, after the first one is complete. This is useful when the execution must complete within a certain time period, but several steps are needed. The waitFor-type functions in QIODevice and its subclasses are good examples of such need. In that case, the code could be as follows:

void executeSlowOperations(int timeout) { QElapsedTimer timer; timer.start(); slowOperation1();

int remainingTime = timeout - timer.elapsed(); if (remainingTime > 0) slowOperation2(remainingTime); }

Another use-case is to execute a certain operation for a specific timeslice. For this, QElapsedTimer provides the hasExpired() convenience function, which can be used to determine if a certain number of milliseconds has already elapsed:

void executeOperationsForTime(int ms) { QElapsedTimer timer; timer.start();

while (!timer.hasExpired(ms)) slowOperation1(); }

It is often more convenient to use QDeadlineTimer in this case, which counts towards a timeout in the future instead of tracking elapsed time.

source

pub unsafe fn nsecs_elapsed(&self) -> i64

Returns the number of nanoseconds since this QElapsedTimer was last started.

Calls C++ function: qint64 QElapsedTimer::nsecsElapsed() const.

C++ documentation:

Returns the number of nanoseconds since this QElapsedTimer was last started.

Calling this function on a QElapsedTimer that is invalid results in undefined behavior.

On platforms that do not provide nanosecond resolution, the value returned will be the best estimate available.

This function was introduced in Qt 4.8.

See also start(), restart(), hasExpired(), and invalidate().

source

pub unsafe fn restart(&self) -> i64

Restarts the timer and returns the time elapsed since the previous start. This function is equivalent to obtaining the elapsed time with elapsed() and then starting the timer again with start(), but it does so in one single operation, avoiding the need to obtain the clock value twice.

Calls C++ function: qint64 QElapsedTimer::restart().

C++ documentation:

Restarts the timer and returns the time elapsed since the previous start. This function is equivalent to obtaining the elapsed time with elapsed() and then starting the timer again with start(), but it does so in one single operation, avoiding the need to obtain the clock value twice.

Calling this function on a QElapsedTimer that is invalid results in undefined behavior.

The following example illustrates how to use this function to calibrate a parameter to a slow operation (for example, an iteration count) so that this operation takes at least 250 milliseconds:

QElapsedTimer timer;

int count = 1; timer.start(); do { count *= 2; slowOperation2(count); } while (timer.restart() < 250);

return count;

See also start(), invalidate(), elapsed(), and isValid().

source

pub unsafe fn secs_to(&self, other: impl CastInto<Ref<QElapsedTimer>>) -> i64

Returns the number of seconds between this QElapsedTimer and other. If other was started before this object, the returned value will be negative. If it was started later, the returned value will be positive.

Calls C++ function: qint64 QElapsedTimer::secsTo(const QElapsedTimer& other) const.

C++ documentation:

Returns the number of seconds between this QElapsedTimer and other. If other was started before this object, the returned value will be negative. If it was started later, the returned value will be positive.

Calling this function on or with a QElapsedTimer that is invalid results in undefined behavior.

See also msecsTo() and elapsed().

source

pub unsafe fn start(&self)

Starts this timer. Once started, a timer value can be checked with elapsed() or msecsSinceReference().

Calls C++ function: void QElapsedTimer::start().

C++ documentation:

Starts this timer. Once started, a timer value can be checked with elapsed() or msecsSinceReference().

Normally, a timer is started just before a lengthy operation, such as:

QElapsedTimer timer; timer.start();

slowOperation1();

qDebug() << “The slow operation took” << timer.elapsed() << “milliseconds”;

Also, starting a timer makes it valid again.

See also restart(), invalidate(), and elapsed().

Trait Implementations§

source§

impl CppDeletable for QElapsedTimer

source§

unsafe fn delete(&self)

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

Calls C++ function: [destructor] void QElapsedTimer::~QElapsedTimer().

C++ documentation:

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

The QElapsedTimer class is usually used to quickly calculate how much time has elapsed between two events. Its API is similar to that of QTime, so code that was using that can be ported quickly to the new class.

However, unlike QTime, QElapsedTimer tries to use monotonic clocks if possible. This means it's not possible to convert QElapsedTimer objects to a human-readable time.

The typical use-case for the class is to determine how much time was spent in a slow operation. The simplest example of such a case is for debugging purposes, as in the following example:

QElapsedTimer timer; timer.start();

slowOperation1();

qDebug() << “The slow operation took” << timer.elapsed() << “milliseconds”;

In this example, the timer is started by a call to start() and the elapsed timer is calculated by the elapsed() function.

The time elapsed can also be used to recalculate the time available for another operation, after the first one is complete. This is useful when the execution must complete within a certain time period, but several steps are needed. The waitFor-type functions in QIODevice and its subclasses are good examples of such need. In that case, the code could be as follows:

void executeSlowOperations(int timeout) { QElapsedTimer timer; timer.start(); slowOperation1();

int remainingTime = timeout - timer.elapsed(); if (remainingTime > 0) slowOperation2(remainingTime); }

Another use-case is to execute a certain operation for a specific timeslice. For this, QElapsedTimer provides the hasExpired() convenience function, which can be used to determine if a certain number of milliseconds has already elapsed:

void executeOperationsForTime(int ms) { QElapsedTimer timer; timer.start();

while (!timer.hasExpired(ms)) slowOperation1(); }

It is often more convenient to use QDeadlineTimer in this case, which counts towards a timeout in the future instead of tracking elapsed time.

source§

impl PartialEq<Ref<QElapsedTimer>> for QElapsedTimer

source§

fn eq(&self, other: &Ref<QElapsedTimer>) -> bool

Returns true if this object and other contain the same time.

Calls C++ function: bool QElapsedTimer::operator==(const QElapsedTimer& other) const.

C++ documentation:

Returns true if this object and other contain the same time.

1.0.0 · source§

fn ne(&self, other: &Rhs) -> bool

This method tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.

Auto Trait Implementations§

Blanket Implementations§

source§

impl<T> Any for T
where T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

impl<T> Borrow<T> for T
where T: ?Sized,

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
source§

impl<T, U> CastInto<U> for T
where U: CastFrom<T>,

source§

unsafe fn cast_into(self) -> U

Performs the conversion. Read more
source§

impl<T> From<T> for T

source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

impl<T, U> Into<U> for T
where U: From<T>,

source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

source§

impl<T> StaticUpcast<T> for T

source§

unsafe fn static_upcast(ptr: Ptr<T>) -> Ptr<T>

Convert type of a const pointer. Read more
source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

§

type Error = Infallible

The type returned in the event of a conversion error.
source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.