Skip to content

PythonQt.Qt.QElapsedTimer

QElapsedTimer

PythonQtInstanceWrapper object

Native bases: PythonQtInstanceWrapper

Supplemental documentation: Qt 5 reference:

Detailed Description

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 time 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.

Reference Clocks

QElapsedTimer will use the platform's monotonic reference clock in all platforms that support it (see QElapsedTimer::isMonotonic()). This has the added benefit that QElapsedTimer is immune to time adjustments, such as the user correcting the time. Also unlike QTime, QElapsedTimer is immune to changes in the timezone settings, such as daylight-saving periods.

On the other hand, this means QElapsedTimer values can only be compared with other values that use the same reference. This is especially true if the time since the reference is extracted from the QElapsedTimer object (QElapsedTimer::msecsSinceReference()) and serialised. These values should never be exchanged across the network or saved to disk, since there's no telling whether the computer node receiving the data is the same as the one originating it or if it has rebooted since.

It is, however, possible to exchange the value with other processes running on the same machine, provided that they also use the same reference clock. QElapsedTimer will always use the same clock, so it's safe to compare with the value coming from another process in the same machine. If comparing to values produced by other APIs, you should check that the clock used is the same as QElapsedTimer (see QElapsedTimer::clockType()).

Source: https://doc.qt.io/archives/qt-5.15/qelapsedtimer.html

Complete member index

Every captured member retains its own identity. Identical captured declaration text is linked once; this does not establish native inheritance or interchangeable objects.

Member Kind Signature Summary
ClockType attribute type Convert a number or string to an integer, or return 0 if no arguments are given. (shared captured text)
MachAbsoluteTime attribute Any (shared captured text)
MonotonicClock attribute Any (shared captured text)
PerformanceCounter attribute Any (shared captured text)
SystemTime attribute Any (shared captured text)
TickCounter attribute Any (shared captured text)
__init__ function (self, *args: Any, **kwargs: Any) -> None Native constructor; consult class documentation for supported overloads. (shared captured text)
className function (self, *args: Any, **kwargs: Any) -> Any Return the classname of the object (shared captured text)
clockType function (self) -> Any (shared captured text)
delete function (self, *args: Any, **kwargs: Any) -> Any Deletes the C++ object (at your own risk, my friend!) (shared captured text)
elapsed function (self) -> int X.elapsed() -> int (shared captured text)
hasExpired function (self, timeout: int) -> bool X.hasExpired(timeout) -> bool (shared captured text)
help function (self, *args: Any, **kwargs: Any) -> Any Shows the help of available methods for this class (shared captured text)
inherits function (self, *args: Any, **kwargs: Any) -> Any Returns if the class inherits or is of given type name (shared captured text)
invalidate function (self) -> None (shared captured text)
isMonotonic function (self) -> bool X.isMonotonic() -> bool (shared captured text)
isValid function (self) -> bool X.isValid() -> bool (shared captured text)
msecsSinceReference function (self) -> int X.msecsSinceReference() -> int (shared captured text)
msecsTo function (self, other: Any) -> int X.msecsTo(other) -> int (shared captured text)
nsecsElapsed function (self) -> int X.nsecsElapsed() -> int (shared captured text)
restart function (self) -> int X.restart() -> int (shared captured text)
secsTo function (self, other: Any) -> int X.secsTo(other) -> int (shared captured text)
start function (self) -> None (shared captured text)