PySide6.QtCore.QDeadlineTimer¶
- class QDeadlineTimer¶
The
QDeadlineTimerclass marks a deadline in the future. More…Synopsis¶
Methods¶
def
__init__()def
deadline()def
deadlineNSecs()def
hasExpired()def
isForever()def
__ne__()def
__add__()def
__iadd__()def
__sub__()def
__isub__()def
__lt__()def
__le__()def
__eq__()def
__gt__()def
__ge__()def
remainingTime()def
setDeadline()def
setTimerType()def
swap()def
timerType()
Static functions¶
def
addNSecs()def
current()
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
QDeadlineTimerclass is usually used to calculate future deadlines and verify whether the deadline has expired.QDeadlineTimercan also be used for deadlines without expiration (“forever”). It forms a counterpart toQElapsedTimer, which calculates how much time has elapsed sincestart()was called.QDeadlineTimerprovides a more convenient API compared tohasExpired().The typical use-case for the class is to create a
QDeadlineTimerbefore the operation in question is started, and then useremainingTime()orhasExpired()to determine whether to continue trying the operation.QDeadlineTimerobjects 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
QDeadlineTimerfunctions deal with time out values, which all are measured in milliseconds. There are two special values, the same as many other Qt functions namedwaitForor similar:0: no time left, expired
-1: infinite time left, timer never expires
Reference Clocks¶
QDeadlineTimerwill use the same clock asQElapsedTimer(seeclockType()andisMonotonic()).Timer types¶
Like
QTimerandQChronoTimer,QDeadlineTimercan select among different levels of coarseness on the timers. You can select precise timing by passingPreciseTimerto the functions that set of change the timer, or you can select coarse timing by passingCoarseTimer.VeryCoarseTimeris currently interpreted the same way asCoarseTimer.This feature is dependent on support from the operating system: if the OS does not support a coarse timer functionality, then
QDeadlineTimerwill behave likePreciseTimerwas passed.QDeadlineTimerdefaults toCoarseTimerbecause 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¶
QDeadlineTimeris compatible with thestd::chronoAPI from C++11 and can be constructed from or compared to bothstd::chrono::durationandstd::chrono::time_pointobjects. In addition, it is fully compatible with the time literals from C++14, which allow one to write code such as:deadline = QDeadlineTimer(30s) device.waitForReadyRead(deadline) if deadline.remainingTime<std.chrono.nanoseconds>() > 300ms: cleanup()
As can be seen in the example above,
QDeadlineTimeroffers a templated version ofremainingTime()anddeadline()that can be used to returnstd::chronoobjects.Note that comparing to
time_pointis not as efficient as comparing toduration, sinceQDeadlineTimermay need to convert from its own internal clock source to the clock source used by thetime_pointobject. Also note that, due to this conversion, the deadlines will not be precise, so the following code is not expected to compare equally:now = std::chrono::steady_clock::now() deadline = QDeadlineTimer(now + 1s) Q_ASSERT(deadline == now + 1s)
See also
QTimeQChronoTimerQElapsedTimerTimerType- class ForeverConstant¶
Constant
Description
QDeadlineTimer.ForeverConstant.ForeverConstant.Forever
Used when creating a
QDeadlineTimerto indicate the deadline should not expire
- PySide6.QtCore.QDeadlineTimer.Forever¶
- __init__()¶
- __init__(type_)
- Parameters:
type –
TimerType
- __init__(arg__1[, type_=Qt.CoarseTimer])
- Parameters:
arg__1 –
ForeverConstanttype –
TimerType
QDeadlineTimerobjects created withForeverConstantnever expire. For such objects,remainingTime()will return -1,deadline()will return the maximum value, andisForever()will return true.The timer type
timerTypemay be ignored, since the timer will never expire.- __init__(msecs[, type=Qt.CoarseTimer])
- Parameters:
msecs – int
type –
TimerType
Constructs a
QDeadlineTimerobject with an expiry time ofmsecsmsecs from the moment of the creation of this object, if msecs is positive. Ifmsecsis zero, thisQDeadlineTimerwill be marked as expired, causingremainingTime()to return zero anddeadline()to return an indeterminate time point in the past. Ifmsecsis negative, the timer will be set to never expire, causingremainingTime()to return -1 anddeadline()to return the maximum value.The
QDeadlineTimerobject will be constructed with the specified timertype.For optimization purposes, if
msecsis 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, usecurrent()and add time to it.Note
Prior to Qt 6.6, the only value that caused the timer to never expire was -1.
- static addNSecs(dt, nsecs)¶
- Parameters:
dt –
QDeadlineTimernsecs – int
- Return type:
Returns a
QDeadlineTimerobject whose deadline is extended fromdt's deadline bynsecsnanoseconds. Ifdtwas set to never expire, this function returns aQDeadlineTimerthat will not expire either.Note
if
dtwas created as expired, its deadline is indeterminate and adding an amount of time may or may not cause it to become unexpired.Returns a
QDeadlineTimerthat 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 thedeadline()function.The
QDeadlineTimerobject will be constructed with the specifiedtimerType.- 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
QDeadlineTimerobject, calculated in milliseconds relative to the reference clock, the same asmsecsSinceReference(). The value will be in the past if thisQDeadlineTimerhas expired.If this
QDeadlineTimernever expires, this function returnsstd::numeric_limits<qint64>::max().This function can be used to calculate the amount of time a timer is overdue, by subtracting
current()ormsecsSinceReference(), 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.
See also
- 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
QDeadlineTimerobject, calculated in nanoseconds relative to the reference clock, the same asmsecsSinceReference(). The value will be in the past if thisQDeadlineTimerhas expired.If this
QDeadlineTimernever expires or the number of nanoseconds until the deadline can’t be accommodated in the return type, this function returnsstd::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.
See also
- hasExpired()¶
- Return type:
bool
Returns true if this
QDeadlineTimerobject has expired, false if there remains time left. For objects that have expired,remainingTime()will return zero anddeadline()will return a time point in the past.QDeadlineTimerobjects created with theForeverConstantnever expire and this function always returns false for them.See also
- isForever()¶
- Return type:
bool
Returns true if this
QDeadlineTimerobject never expires, false otherwise. For timers that never expire,remainingTime()always returns -1 anddeadline()returns the maximum value.See also
- __ne__(rhs)¶
- Parameters:
rhs –
QDeadlineTimer- 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
lhsand the deadline inrhsare different, false otherwise. The timer type used to create the two deadlines is ignored. This function is equivalent to:return lhs.deadlineNSecs() != rhs.deadlineNSecs()
Note
comparing
QDeadlineTimerobjects with different timer types is not supported and may result in unpredictable behavior.- __add__(msecs)¶
- Parameters:
msecs – int
- Return type:
Returns a
QDeadlineTimerobject whose deadline ismsecslater than the deadline stored indt. Ifdtis set to never expire, this function returns aQDeadlineTimerthat does not expire either.To add times of precision greater than 1 millisecond, use
addNSecs().- __iadd__(msecs)¶
- Parameters:
msecs – int
- Return type:
Extends this
QDeadlineTimerobject bymsecsmilliseconds 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:
dt2 –
QDeadlineTimer- Return type:
int
- __sub__(msecs)
- Parameters:
msecs – int
- Return type:
Returns a
QDeadlineTimerobject whose deadline ismsecsbefore the deadline stored indt. Ifdtis set to never expire, this function returns aQDeadlineTimerthat does not expire either.To subtract times of precision greater than 1 millisecond, use
addNSecs().- __isub__(msecs)¶
- Parameters:
msecs – int
- Return type:
Shortens this
QDeadlineTimerobject bymsecsmilliseconds 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__(rhs)¶
- Parameters:
rhs –
QDeadlineTimer- 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
lhsis earlier than the deadline inrhs, false otherwise. The timer type used to create the two deadlines is ignored. This function is equivalent to:return lhs.deadlineNSecs() < rhs.deadlineNSecs()
Note
comparing
QDeadlineTimerobjects with different timer types is not supported and may result in unpredictable behavior.- __le__(rhs)¶
- Parameters:
rhs –
QDeadlineTimer- 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
lhsis earlier than or the same as the deadline inrhs, false otherwise. The timer type used to create the two deadlines is ignored. This function is equivalent to:return lhs.deadlineNSecs() <= rhs.deadlineNSecs()
Note
comparing
QDeadlineTimerobjects with different timer types is not supported and may result in unpredictable behavior.- __eq__(rhs)¶
- Parameters:
rhs –
QDeadlineTimer- 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
lhsand the deadline inrhsare the same, false otherwise. The timer type used to create the two deadlines is ignored. This function is equivalent to:return lhs.deadlineNSecs() == rhs.deadlineNSecs()
Note
comparing
QDeadlineTimerobjects with different timer types is not supported and may result in unpredictable behavior.- __gt__(rhs)¶
- Parameters:
rhs –
QDeadlineTimer- 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
lhsis later than the deadline inrhs, false otherwise. The timer type used to create the two deadlines is ignored. This function is equivalent to:return lhs.deadlineNSecs() > rhs.deadlineNSecs()
Note
comparing
QDeadlineTimerobjects with different timer types is not supported and may result in unpredictable behavior.- __ge__(rhs)¶
- Parameters:
rhs –
QDeadlineTimer- 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
lhsis later than or the same as the deadline inrhs, false otherwise. The timer type used to create the two deadlines is ignored. This function is equivalent to:return lhs.deadlineNSecs() >= rhs.deadlineNSecs()
Note
comparing
QDeadlineTimerobjects 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
QDeadlineTimerobject 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, seedeadline()). 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
QIODevicewaitForfunctions or the timed lock functions inQMutex,QWaitCondition,QSemaphore, orQReadWriteLock. For example:mutex.tryLock(deadline.remainingTime())
- remainingTimeNSecs()¶
- Return type:
int
Returns the remaining time in this
QDeadlineTimerobject 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.See also
Sets the deadline for this
QDeadlineTimerobject to be themsecsabsolute time point, counted in milliseconds since the reference clock (the same asmsecsSinceReference()), and the timer type totimerType. If the value is in the past, thisQDeadlineTimerwill be marked as expired.If
msecsisstd::numeric_limits<qint64>::max()or the deadline is beyond a representable point in the future, thisQDeadlineTimerwill be set to never expire.- setPreciseDeadline(secs[, nsecs=0[, type=Qt.CoarseTimer]])¶
- Parameters:
secs – int
nsecs – int
type –
TimerType
Sets the deadline for this
QDeadlineTimerobject to besecsseconds andnsecsnanoseconds since the reference clock epoch (the same asmsecsSinceReference()), and the timer type totimerType. If the value is in the past, thisQDeadlineTimerwill be marked as expired.If
secsornsecsisstd::numeric_limits<qint64>::max(), thisQDeadlineTimerwill be set to never expire. Ifnsecsis more than 1 billion nanoseconds (1 second), thensecswill be adjusted accordingly.- setPreciseRemainingTime(secs[, nsecs=0[, type=Qt.CoarseTimer]])¶
- Parameters:
secs – int
nsecs – int
type –
TimerType
Sets the remaining time for this
QDeadlineTimerobject tosecsseconds plusnsecsnanoseconds from now, ifsecshas a positive value. Ifsecsis negative, thisQDeadlineTimerwill be set it to never expire (this behavior does not apply tonsecs). If both parameters are zero, thisQDeadlineTimerwill be marked as expired.For optimization purposes, if both
secsandnsecsare 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, usecurrent()and add time to it.The timer type for this
QDeadlineTimerobject will be set to the specifiedtimerType.Note
Prior to Qt 6.6, the only condition that caused the timer to never expire was when
secswas -1.Sets the remaining time for this
QDeadlineTimerobject tomsecsmilliseconds from now, ifmsecshas a positive value. Ifmsecsis zero, thisQDeadlineTimerobject will be marked as expired, whereas a negative value will set it to never expire.For optimization purposes, if
msecsis 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, usecurrent()and add time to it.The timer type for this
QDeadlineTimerobject will be set to the specifiedtimerType.Note
Prior to Qt 6.6, the only value that caused the timer to never expire was -1.
Changes the timer type for this object to
timerType.The behavior for each possible value of
timerTypeis operating-system dependent.PreciseTimerwill use the most precise timer that Qt can find, with resolution of 1 millisecond or better, whereasQDeadlineTimerwill try to use a more coarse timer forCoarseTimerandVeryCoarseTimer.See also
- swap(other)¶
- Parameters:
other –
QDeadlineTimer
Swaps this deadline timer with
other. This operation is very fast and never fails.Returns the timer type is active for this object.
See also