QDeadlineTimerclass marks a deadline in the future. 更多 …
New in version 5.8.
def
__iadd__
(msecs)
def
__isub__
(msecs)
def
_q_data
()
def
deadline
()
def
deadlineNSecs
()
def
hasExpired
()
def
isForever
()
def
remainingTime
()
def
remainingTimeNSecs
()
def
setDeadline
(msecs[, timerType=Qt.CoarseTimer])
def
setPreciseDeadline
(secs[, nsecs=0[, type=Qt.CoarseTimer]])
def
setPreciseRemainingTime
(secs[, nsecs=0[, type=Qt.CoarseTimer]])
def
setRemainingTime
(msecs[, type=Qt.CoarseTimer])
def
setTimerType
(type)
def
swap
(other)
def
timerType
()
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,计算已消耗多少时间从start()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.void executeOperation(int msecs) { QDeadlineTimer deadline(msecs); do { if (readFromDevice(deadline.remainingTime()) break; waitForReadyRead(deadline); } while (!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 namedwaitFor或类似:
0:没有剩余时间,已过期
-1:无限剩余时间,定时器从不过期
QDeadlineTimerwill use the same clock asQElapsedTimer(见clockType()andisMonotonic()).
像
QTimer,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.
QDeadlineTimer默认为CoarseTimerbecause 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
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 as:using namespace std::chrono; using namespace std::chrono_literals; QDeadlineTimer deadline(30s); device->waitForReadyRead(deadline); if (deadline.remainingTime<nanoseconds>() > 300ms) cleanup();As can be seen in the example above,
QDeadlineTimeroffers a templated version ofremainingTime()anddeadline()that can be used to returnstd::chrono对象。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:using namespace std::chrono; using namespace std::chrono_literals; auto now = steady_clock::now(); QDeadlineTimer deadline(now + 1s); Q_ASSERT(deadline == now + 1s);另请参阅
QTimeQTimerQDeadlineTimerTimerType
QDeadlineTimer
(
arg__1
[
,
type_=Qt.CoarseTimer
]
)
¶
QDeadlineTimer([type_=Qt.CoarseTimer])
QDeadlineTimer(QDeadlineTimer)
QDeadlineTimer(msecs[, type=Qt.CoarseTimer])
- param type
TimerType- param arg__1
ForeverConstant- param msecs
qint64- param type_
TimerType- param QDeadlineTimer
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.
另请参阅
ForeverConstant
hasExpired()
isForever()
remainingTime()
timerType()
Constructs an expired
QDeadlineTimer
object. For this object,
remainingTime()
will return 0.
The timer type
timerType
may be ignored, since the timer is already expired. Similarly, for optimization purposes, this function will not attempt to obtain the current time and will use a value known to be in the past. Therefore,
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()
.
另请参阅
hasExpired()
remainingTime()
TimerType
current()
构造
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 -1, the timer will be set to never expire, causing
remainingTime()
to return -1 and
deadline()
to return the maximum value.
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.
PySide2.QtCore.QDeadlineTimer.
ForeverConstant
¶
|
常量 |
描述 |
|---|---|
|
QDeadlineTimer.Forever |
使用当创建
|
PySide2.QtCore.QDeadlineTimer.
_q_data
(
)
¶
PySide2.QtCore.QDeadlineTimer.
addNSecs
(
dt
,
nsecs
)
¶
dt
–
QDeadlineTimer
nsecs
–
qint64
返回
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.
注意
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.
PySide2.QtCore.QDeadlineTimer.
current
(
[
timerType=Qt.CoarseTimer
]
)
¶
timerType
–
TimerType
返回
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()
函数。
QDeadlineTimer
object will be constructed with the specified
timerType
.
PySide2.QtCore.QDeadlineTimer.
deadline
(
)
¶
qint64
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.
若此
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:
qint64 realTimeLeft = deadline.deadline();
if (realTimeLeft != (std::numeric_limits<qint64>::max)()) {
realTimeLeft -= QDeadlineTimer::current().deadline();
// or:
//QElapsedTimer timer;
//timer.start();
//realTimeLeft -= timer.msecsSinceReference();
}
注意
Timers that were created as expired have an indetermine time point in the past as their deadline, so the above calculation may not work.
PySide2.QtCore.QDeadlineTimer.
deadlineNSecs
(
)
¶
qint64
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.
若此
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:
qint64 realTimeLeft = deadline.deadlineNSecs();
if (realTimeLeft != std::numeric_limits<qint64>::max())
realTimeLeft -= QDeadlineTimer::current().deadlineNSecs();
注意
Timers that were created as expired have an indetermine time point in the past as their deadline, so the above calculation may not work.
PySide2.QtCore.QDeadlineTimer.
hasExpired
(
)
¶
bool
返回 true,若此
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.
PySide2.QtCore.QDeadlineTimer.
isForever
(
)
¶
bool
返回 true,若此
QDeadlineTimer
object never expires, false otherwise. For timers that never expire,
remainingTime()
always returns -1 and
deadline()
returns the maximum value.
另请参阅
ForeverConstant
hasExpired()
remainingTime()
PySide2.QtCore.QDeadlineTimer.
__iadd__
(
msecs
)
¶
msecs
–
qint64
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()
.
PySide2.QtCore.QDeadlineTimer.
__isub__
(
msecs
)
¶
msecs
–
qint64
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()
.
PySide2.QtCore.QDeadlineTimer.
remainingTime
(
)
¶
qint64
返回剩余时间在此
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
,或
QReadWriteLock
。例如:
mutex.tryLock(deadline.remainingTime());
PySide2.QtCore.QDeadlineTimer.
remainingTimeNSecs
(
)
¶
qint64
返回剩余时间在此
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.
PySide2.QtCore.QDeadlineTimer.
setDeadline
(
msecs
[
,
timerType=Qt.CoarseTimer
]
)
¶
msecs
–
qint64
timerType
–
TimerType
设置截止日期为此
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.
若
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.
PySide2.QtCore.QDeadlineTimer.
setPreciseDeadline
(
secs
[
,
nsecs=0
[
,
type=Qt.CoarseTimer
]
]
)
¶
secs
–
qint64
nsecs
–
qint64
type
–
TimerType
设置截止日期为此
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.
若
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.
PySide2.QtCore.QDeadlineTimer.
setPreciseRemainingTime
(
secs
[
,
nsecs=0
[
,
type=Qt.CoarseTimer
]
]
)
¶
secs
–
qint64
nsecs
–
qint64
type
–
TimerType
设置剩余时间为此
QDeadlineTimer
对象到
secs
seconds plus
nsecs
nanoseconds from now, if
secs
has a positive value. If
secs
is -1, this
QDeadlineTimer
will be set it to never expire. If both parameters are zero, this
QDeadlineTimer
will be marked as expired.
The timer type for this
QDeadlineTimer
object will be set to the specified
timerType
.
PySide2.QtCore.QDeadlineTimer.
setRemainingTime
(
msecs
[
,
type=Qt.CoarseTimer
]
)
¶
msecs
–
qint64
type
–
TimerType
设置剩余时间为此
QDeadlineTimer
对象到
msecs
milliseconds from now, if
msecs
has a positive value. If
msecs
is zero, this
QDeadlineTimer
object will be marked as expired, whereas a value of -1 will set it to never expire.
The timer type for this
QDeadlineTimer
object will be set to the specified
timerType
.
PySide2.QtCore.QDeadlineTimer.
setTimerType
(
type
)
¶
type
–
TimerType
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
.
另请参阅
timerType()
TimerType
PySide2.QtCore.QDeadlineTimer.
swap
(
other
)
¶
other
–
QDeadlineTimer
Swaps this deadline timer with the
other
deadline timer.
PySide2.QtCore.QDeadlineTimer.
timerType
(
)
¶
TimerType
Returns the timer type is active for this object.
另请参阅