class QDeadlineTimer#

The QDeadlineTimer class marks a deadline in the future. More

Synopsis#

Methods#

Static functions#

Note

This documentation may contain snippets that were automatically translated from C++ to Python. We always welcome contributions to the snippet translation. If you see an issue with the translation, you can also let us know by creating a ticket on https:/bugreports.qt.io/projects/PYSIDE

Detailed Description#

Warning

This section contains snippets that were automatically translated from C++ to Python and may contain errors.

The QDeadlineTimer class is usually used to calculate future deadlines and verify whether the deadline has expired. QDeadlineTimer can also be used for deadlines without expiration (“forever”). It forms a counterpart to QElapsedTimer , which calculates how much time has elapsed since start() was called.

QDeadlineTimer provides a more convenient API compared to hasExpired() .

The typical use-case for the class is to create a QDeadlineTimer before the operation in question is started, and then use remainingTime() or hasExpired() to determine whether to continue trying the operation. QDeadlineTimer objects can be passed to functions being called to execute this operation so they know how long to still operate.

def executeOperation(msecs):

    deadline = QDeadlineTimer(msecs)
    do {
        if readFromDevice(deadline.remainingTime()):
            break
        waitForReadyRead(deadline)
    } while (not deadline.hasExpired())

Many QDeadlineTimer functions deal with time out values, which all are measured in milliseconds. There are two special values, the same as many other Qt functions named waitFor or similar:

  • 0: no time left, expired

  • -1: infinite time left, timer never expires

Reference Clocks#

QDeadlineTimer will use the same clock as QElapsedTimer (see clockType() and isMonotonic() ).

Timer types#

Like QTimer , QDeadlineTimer can select among different levels of coarseness on the timers. You can select precise timing by passing PreciseTimer to the functions that set of change the timer, or you can select coarse timing by passing CoarseTimer . VeryCoarseTimer is currently interpreted the same way as CoarseTimer .

This feature is dependent on support from the operating system: if the OS does not support a coarse timer functionality, then QDeadlineTimer will behave like PreciseTimer was passed.

QDeadlineTimer defaults to CoarseTimer because on operating systems that do support coarse timing, making timing calls to that clock source is often much more efficient. The level of coarseness depends on the operating system, but should be in the order of a couple of milliseconds.

`` std::chrono``

Compatibility#

QDeadlineTimer is compatible with the std::chrono API from C++11 and can be constructed from or compared to both std::chrono::duration and std::chrono::time_point objects. In addition, it is fully compatible with the time literals from C++14, which allow one to write code as:

namespace = using()
namespace = using()
deadline = QDeadlineTimer(30s)
device.waitForReadyRead(deadline)
if deadline.remainingTime<nanoseconds>() > 300ms:
    cleanup()

As can be seen in the example above, QDeadlineTimer offers a templated version of remainingTime() and deadline() that can be used to return std::chrono objects.

Note that comparing to time_point is not as efficient as comparing to duration, since QDeadlineTimer may need to convert from its own internal clock source to the clock source used by the time_point object. Also note that, due to this conversion, the deadlines will not be precise, so the following code is not expected to compare equally:

namespace = using()
namespace = using()
now = steady_clock::now()
deadline = QDeadlineTimer(now + 1s)
Q_ASSERT(deadline == now + 1s)
class ForeverConstant#

Constant

Description

QDeadlineTimer.ForeverConstant.Forever

Used when creating a QDeadlineTimer to indicate the deadline should not expire

PySide6.QtCore.QDeadlineTimer.Forever#
__init__(arg__1[, type_=Qt.CoarseTimer])#
Parameters:

QDeadlineTimer objects created with ForeverConstant never expire. For such objects, remainingTime() will return -1, deadline() will return the maximum value, and isForever() will return true.

The timer type timerType may be ignored, since the timer will never expire.

__init__(type_)
Parameters:

typeTimerType

__init__(msecs[, type=Qt.CoarseTimer])
Parameters:

Constructs a QDeadlineTimer object with an expiry time of msecs msecs from the moment of the creation of this object, if msecs is positive. If msecs is zero, this QDeadlineTimer will be marked as expired, causing remainingTime() to return zero and deadline() to return an indeterminate time point in the past. If msecs is negative, the timer will be set to never expire, causing remainingTime() to return -1 and deadline() to return the maximum value.

The QDeadlineTimer object will be constructed with the specified timer type.

For optimization purposes, if msecs is zero, this function may skip obtaining the current time and may instead use a value known to be in the past. If that happens, deadline() may return an unexpected value and this object cannot be used in calculation of how long it is overdue. If that functionality is required, use current() and add time to it.

Note

Prior to Qt 6.6, the only value that caused the timer to never expire was -1.

__init__()
static addNSecs(dt, nsecs)#
Parameters:
Return type:

QDeadlineTimer

Returns a QDeadlineTimer object whose deadline is extended from dt's deadline by nsecs nanoseconds. If dt was set to never expire, this function returns a QDeadlineTimer that will not expire either.

Note

if dt was created as expired, its deadline is indeterminate and adding an amount of time may or may not cause it to become unexpired.

static current([timerType=Qt.CoarseTimer])#
Parameters:

timerTypeTimerType

Return type:

QDeadlineTimer

Returns a QDeadlineTimer that is expired but is guaranteed to contain the current time. Objects created by this function can participate in the calculation of how long a timer is overdue, using the deadline() function.

The QDeadlineTimer object will be constructed with the specified timerType.

deadline()#
Return type:

int

Warning

This section contains snippets that were automatically translated from C++ to Python and may contain errors.

Returns the absolute time point for the deadline stored in QDeadlineTimer object, calculated in milliseconds relative to the reference clock, the same as msecsSinceReference() . The value will be in the past if this QDeadlineTimer has expired.

If this QDeadlineTimer never expires, this function returns std::numeric_limits<qint64>::max().

This function can be used to calculate the amount of time a timer is overdue, by subtracting current() or msecsSinceReference() , as in the following example:

realTimeLeft = deadline.deadline()
if realTimeLeft != (std.numeric_limits<qint64>.max)():
    realTimeLeft -= QDeadlineTimer.current().deadline()
    # or:
    #QElapsedTimer timer
    #timer.start()
    #realTimeLeft -= timer.msecsSinceReference()

Note

Timers that were created as expired have an indetermine time point in the past as their deadline, so the above calculation may not work.

deadlineNSecs()#
Return type:

int

Warning

This section contains snippets that were automatically translated from C++ to Python and may contain errors.

Returns the absolute time point for the deadline stored in QDeadlineTimer object, calculated in nanoseconds relative to the reference clock, the same as msecsSinceReference() . The value will be in the past if this QDeadlineTimer has expired.

If this QDeadlineTimer never expires or the number of nanoseconds until the deadline can’t be accommodated in the return type, this function returns std::numeric_limits<qint64>::max().

This function can be used to calculate the amount of time a timer is overdue, by subtracting current() , as in the following example:

realTimeLeft = deadline.deadlineNSecs()
if realTimeLeft != std.numeric_limits<qint64>.max():
    realTimeLeft -= QDeadlineTimer.current().deadlineNSecs()

Note

Timers that were created as expired have an indetermine time point in the past as their deadline, so the above calculation may not work.

hasExpired()#
Return type:

bool

Returns true if this QDeadlineTimer object has expired, false if there remains time left. For objects that have expired, remainingTime() will return zero and deadline() will return a time point in the past.

QDeadlineTimer objects created with the ForeverConstant never expire and this function always returns false for them.

isForever()#
Return type:

bool

Returns true if this QDeadlineTimer object never expires, false otherwise. For timers that never expire, remainingTime() always returns -1 and deadline() returns the maximum value.

__ne__(d2)#
Parameters:

d2QDeadlineTimer

Return type:

bool

Warning

This section contains snippets that were automatically translated from C++ to Python and may contain errors.

Returns true if the deadline on d1 and the deadline in d2 are different, false otherwise. The timer type used to create the two deadlines is ignored. This function is equivalent to:

return d1.deadlineNSecs() != d2.deadlineNSecs()

Note

comparing QDeadlineTimer objects with different timer types is not supported and may result in unpredictable behavior.

__add__(msecs)#
Parameters:

msecs – int

Return type:

QDeadlineTimer

Returns a QDeadlineTimer object whose deadline is msecs later than the deadline stored in dt. If dt is set to never expire, this function returns a QDeadlineTimer that does not expire either.

To add times of precision greater than 1 millisecond, use addNSecs() .

__iadd__(msecs)#
Parameters:

msecs – int

Return type:

QDeadlineTimer

Extends this QDeadlineTimer object by msecs milliseconds and returns itself. If this object is set to never expire, this function does nothing.

To add times of precision greater than 1 millisecond, use addNSecs() .

__sub__(dt2)#
Parameters:

dt2QDeadlineTimer

Return type:

int

__sub__(msecs)
Parameters:

msecs – int

Return type:

QDeadlineTimer

Returns a QDeadlineTimer object whose deadline is msecs before the deadline stored in dt. If dt is set to never expire, this function returns a QDeadlineTimer that does not expire either.

To subtract times of precision greater than 1 millisecond, use addNSecs() .

__isub__(msecs)#
Parameters:

msecs – int

Return type:

QDeadlineTimer

Shortens this QDeadlineTimer object by msecs milliseconds and returns itself. If this object is set to never expire, this function does nothing.

To subtract times of precision greater than 1 millisecond, use addNSecs() .

__lt__(d2)#
Parameters:

d2QDeadlineTimer

Return type:

bool

Warning

This section contains snippets that were automatically translated from C++ to Python and may contain errors.

Returns true if the deadline on d1 is earlier than the deadline in d2, false otherwise. The timer type used to create the two deadlines is ignored. This function is equivalent to:

return d1.deadlineNSecs() < d2.deadlineNSecs()

Note

comparing QDeadlineTimer objects with different timer types is not supported and may result in unpredictable behavior.

__le__(d2)#
Parameters:

d2QDeadlineTimer

Return type:

bool

Warning

This section contains snippets that were automatically translated from C++ to Python and may contain errors.

Returns true if the deadline on d1 is earlier than or the same as the deadline in d2, false otherwise. The timer type used to create the two deadlines is ignored. This function is equivalent to:

return d1.deadlineNSecs() <= d2.deadlineNSecs()

Note

comparing QDeadlineTimer objects with different timer types is not supported and may result in unpredictable behavior.

__eq__(d2)#
Parameters:

d2QDeadlineTimer

Return type:

bool

Warning

This section contains snippets that were automatically translated from C++ to Python and may contain errors.

Returns true if the deadline on d1 and the deadline in d2 are the same, false otherwise. The timer type used to create the two deadlines is ignored. This function is equivalent to:

return d1.deadlineNSecs() == d2.deadlineNSecs()

Note

comparing QDeadlineTimer objects with different timer types is not supported and may result in unpredictable behavior.

__gt__(d2)#
Parameters:

d2QDeadlineTimer

Return type:

bool

Warning

This section contains snippets that were automatically translated from C++ to Python and may contain errors.

Returns true if the deadline on d1 is later than the deadline in d2, false otherwise. The timer type used to create the two deadlines is ignored. This function is equivalent to:

return d1.deadlineNSecs() > d2.deadlineNSecs()

Note

comparing QDeadlineTimer objects with different timer types is not supported and may result in unpredictable behavior.

__ge__(d2)#
Parameters:

d2QDeadlineTimer

Return type:

bool

Warning

This section contains snippets that were automatically translated from C++ to Python and may contain errors.

Returns true if the deadline on d1 is later than or the same as the deadline in d2, false otherwise. The timer type used to create the two deadlines is ignored. This function is equivalent to:

return d1.deadlineNSecs() >= d2.deadlineNSecs()

Note

comparing QDeadlineTimer objects with different timer types is not supported and may result in unpredictable behavior.

remainingTime()#
Return type:

int

Warning

This section contains snippets that were automatically translated from C++ to Python and may contain errors.

Returns the remaining time in this QDeadlineTimer object in milliseconds. If the timer has already expired, this function will return zero and it is not possible to obtain the amount of time overdue with this function (to do that, see deadline() ). If the timer was set to never expire, this function returns -1.

This function is suitable for use in Qt APIs that take a millisecond timeout, such as the many QIODevice waitFor functions or the timed lock functions in QMutex , QWaitCondition , QSemaphore , or QReadWriteLock . For example:

mutex.tryLock(deadline.remainingTime())
remainingTimeNSecs()#
Return type:

int

Returns the remaining time in this QDeadlineTimer object in nanoseconds. If the timer has already expired, this function will return zero and it is not possible to obtain the amount of time overdue with this function. If the timer was set to never expire, this function returns -1.

setDeadline(msecs[, timerType=Qt.CoarseTimer])#
Parameters:

Sets the deadline for this QDeadlineTimer object to be the msecs absolute time point, counted in milliseconds since the reference clock (the same as msecsSinceReference() ), and the timer type to timerType. If the value is in the past, this QDeadlineTimer will be marked as expired.

If msecs is std::numeric_limits<qint64>::max() or the deadline is beyond a representable point in the future, this QDeadlineTimer will be set to never expire.

setPreciseDeadline(secs[, nsecs=0[, type=Qt.CoarseTimer]])#
Parameters:
  • secs – int

  • nsecs – int

  • typeTimerType

Sets the deadline for this QDeadlineTimer object to be secs seconds and nsecs nanoseconds since the reference clock epoch (the same as msecsSinceReference() ), and the timer type to timerType. If the value is in the past, this QDeadlineTimer will be marked as expired.

If secs or nsecs is std::numeric_limits<qint64>::max(), this QDeadlineTimer will be set to never expire. If nsecs is more than 1 billion nanoseconds (1 second), then secs will be adjusted accordingly.

setPreciseRemainingTime(secs[, nsecs=0[, type=Qt.CoarseTimer]])#
Parameters:
  • secs – int

  • nsecs – int

  • typeTimerType

Sets the remaining time for this QDeadlineTimer object to secs seconds plus nsecs nanoseconds from now, if secs has a positive value. If secs is negative, this QDeadlineTimer will be set it to never expire (this behavior does not apply to nsecs). If both parameters are zero, this QDeadlineTimer will be marked as expired.

For optimization purposes, if both secs and nsecs are zero, this function may skip obtaining the current time and may instead use a value known to be in the past. If that happens, deadline() may return an unexpected value and this object cannot be used in calculation of how long it is overdue. If that functionality is required, use current() and add time to it.

The timer type for this QDeadlineTimer object will be set to the specified timerType.

Note

Prior to Qt 6.6, the only condition that caused the timer to never expire was when secs was -1.

setRemainingTime(msecs[, type=Qt.CoarseTimer])#
Parameters:

Sets the remaining time for this QDeadlineTimer object to msecs milliseconds from now, if msecs has a positive value. If msecs is zero, this QDeadlineTimer object will be marked as expired, whereas a negative value will set it to never expire.

For optimization purposes, if msecs is zero, this function may skip obtaining the current time and may instead use a value known to be in the past. If that happens, deadline() may return an unexpected value and this object cannot be used in calculation of how long it is overdue. If that functionality is required, use current() and add time to it.

The timer type for this QDeadlineTimer object will be set to the specified timerType.

Note

Prior to Qt 6.6, the only value that caused the timer to never expire was -1.

setTimerType(type)#
Parameters:

typeTimerType

Changes the timer type for this object to timerType.

The behavior for each possible value of timerType is operating-system dependent. PreciseTimer will use the most precise timer that Qt can find, with resolution of 1 millisecond or better, whereas QDeadlineTimer will try to use a more coarse timer for CoarseTimer and VeryCoarseTimer .

swap(other)#
Parameters:

otherQDeadlineTimer

Swaps this deadline timer with the other deadline timer.

timerType()#
Return type:

TimerType

Returns the timer type is active for this object.

See also

setTimerType()