QDeadlineTimer 类标记未来最后期限。 更多...
头: | #include <QDeadlineTimer> |
CMake: |
find_package(Qt6 REQUIRED COMPONENTS Core)
target_link_libraries(mytarget PRIVATE Qt6::Core) |
qmake: | QT += core |
注意: 此类的所有函数 可重入 .
enum class | ForeverConstant { Forever } |
QDeadlineTimer () | |
QDeadlineTimer (Qt::TimerType timerType ) | |
QDeadlineTimer (QDeadlineTimer::ForeverConstant, Qt::TimerType timerType = Qt::CoarseTimer) | |
QDeadlineTimer (qint64 msecs , Qt::TimerType type = Qt::CoarseTimer) | |
QDeadlineTimer (std::chrono::time_point<Clock, Duration> deadline , Qt::TimerType type = Qt::CoarseTimer) | |
QDeadlineTimer (std::chrono::duration<Rep, Period> remaining , Qt::TimerType type = Qt::CoarseTimer) | |
qint64 | deadline () const |
qint64 | deadlineNSecs () const |
bool | hasExpired () const |
bool | isForever () const |
qint64 | remainingTime () const |
std::chrono::nanoseconds | remainingTimeAsDuration () const |
qint64 | remainingTimeNSecs () const |
void | setDeadline (qint64 msecs , Qt::TimerType timerType = Qt::CoarseTimer) |
void | setDeadline (std::chrono::time_point<Clock, Duration> deadline , Qt::TimerType type = Qt::CoarseTimer) |
void | setPreciseDeadline (qint64 secs , qint64 nsecs = 0, Qt::TimerType timerType = Qt::CoarseTimer) |
void | setPreciseRemainingTime (qint64 secs , qint64 nsecs = 0, Qt::TimerType timerType = Qt::CoarseTimer) |
void | setRemainingTime (qint64 msecs , Qt::TimerType timerType = Qt::CoarseTimer) |
void | setRemainingTime (std::chrono::duration<Rep, Period> remaining , Qt::TimerType type = Qt::CoarseTimer) |
void | setTimerType (Qt::TimerType timerType ) |
void | swap (QDeadlineTimer & other ) |
Qt::TimerType | timerType () const |
QDeadlineTimer & | operator+= (qint64 msecs ) |
QDeadlineTimer & | operator-= (qint64 msecs ) |
QDeadlineTimer & | operator= (std::chrono::time_point<Clock, Duration> deadline_ ) |
QDeadlineTimer & | operator= (std::chrono::duration<Rep, Period> remaining ) |
QDeadlineTimer | addNSecs (QDeadlineTimer dt , qint64 nsecs ) |
QDeadlineTimer | current (Qt::TimerType timerType = Qt::CoarseTimer) |
bool | operator!= (QDeadlineTimer d1 , QDeadlineTimer d2 ) |
QDeadlineTimer | operator+ (QDeadlineTimer dt , qint64 msecs ) |
QDeadlineTimer | operator+ (qint64 msecs , QDeadlineTimer dt ) |
QDeadlineTimer | operator- (QDeadlineTimer dt , qint64 msecs ) |
bool | operator< (QDeadlineTimer d1 , QDeadlineTimer d2 ) |
bool | operator<= (QDeadlineTimer d1 , QDeadlineTimer d2 ) |
bool | operator== (QDeadlineTimer d1 , QDeadlineTimer d2 ) |
bool | operator> (QDeadlineTimer d1 , QDeadlineTimer d2 ) |
bool | operator>= (QDeadlineTimer d1 , QDeadlineTimer d2 ) |
QDeadlineTimer 类通常用于计算未来截止日期并验证截止日期是否已过期。QDeadlineTimer 也可以用于没有过期 forever 的截止日期。它搭档 QElapsedTimer ,计算已消耗多少时间从 QElapsedTimer::start () 被调用。
QDeadlineTimer 提供更方便 API 相比 QElapsedTimer::hasExpired ().
类的典型用例是在有问题操作开始之前创建 QDeadlineTimer,然后使用 remainingTime () 或 hasExpired () 以确定是否继续试着操作。可以把 QDeadlineTimer 对象传递给执行此操作的调用函数,以便它们知道仍要运转多久。
void executeOperation(int msecs) { QDeadlineTimer deadline(msecs); do { if (readFromDevice(deadline.remainingTime())) break; waitForReadyRead(deadline); } while (!deadline.hasExpired()); }
许多 QDeadlineTimer 函数处理超时值,均以毫秒为单位度量。有 2 个特殊值,如同许多其它 Qt 函数
waitFor
或类似:
QDeadlineTimer 将使用相同时钟如 QElapsedTimer (见 QElapsedTimer::clockType () 和 QElapsedTimer::isMonotonic ()).
像 QTimer , QDeadlineTimer can select among different levels of coarseness on the timers. You can select precise timing by passing Qt::PreciseTimer to the functions that set of change the timer, or you can select coarse timing by passing Qt::CoarseTimer . Qt::VeryCoarseTimer is currently interpreted the same way as Qt::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 Qt::PreciseTimer was passed.
QDeadlineTimer defaults to Qt::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
兼容性
QDeadlineTimer 兼容
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:
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, QDeadlineTimer offers a templated version of
remainingTime
() 和
deadline
() that can be used to return
std::chrono
对象。
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:
using namespace std::chrono; using namespace std::chrono_literals; auto now = steady_clock::now(); QDeadlineTimer deadline(now + 1s); Q_ASSERT(deadline == now + 1s);
另请参阅 QTime , QTimer , QDeadlineTimer ,和 Qt::TimerType .
常量 | 值 | 描述 |
---|---|---|
QDeadlineTimer::ForeverConstant::Forever
|
0
|
使用当创建 QDeadlineTimer 以指示截止日期不应该过期 |
[constexpr noexcept]
QDeadlineTimer::
QDeadlineTimer
()
[explicit constexpr noexcept]
QDeadlineTimer::
QDeadlineTimer
(
Qt::TimerType
timerType
)
Constructs an expired QDeadlineTimer object. For this object, remainingTime () will return 0. If timerType is not set, then the object will use the coarse 计时器类型 .
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 QDeadlineTimer::current ().
另请参阅 hasExpired (), remainingTime (), Qt::TimerType ,和 current ().
[constexpr noexcept]
QDeadlineTimer::
QDeadlineTimer
(
QDeadlineTimer::ForeverConstant
,
Qt::TimerType
timerType
= Qt::CoarseTimer)
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 ().
[explicit noexcept]
QDeadlineTimer::
QDeadlineTimer
(
qint64
msecs
,
Qt::TimerType
type
= Qt::CoarseTimer)
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.
QDeadlineTimer 对象将被构造采用指定计时器 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 QDeadlineTimer::current () and add time to it.
注意: Prior to Qt 6.6, the only value that caused the timer to never expire was -1.
另请参阅 hasExpired (), isForever (), remainingTime (),和 setRemainingTime ().
Constructs a QDeadlineTimer object with a deadline at
deadline
time point, converting from the clock source
Clock
to Qt's internal clock source (see
QElapsedTimer::clockType
()).
若
deadline
is in the past, this QDeadlineTimer object is set to expired, whereas if
deadline
等于
Duration::max()
, then this object is set to never expire.
QDeadlineTimer 对象将被构造采用指定计时器 type .
另请参阅 hasExpired (), isForever (), remainingTime (),和 setDeadline ().
构造 QDeadlineTimer 对象采用剩余时间
remaining
。若
remaining
is zero or negative, this QDeadlineTimer object will be mark as expired, whereas if
remaining
等于
duration::max()
, the object will be set to never expire.
QDeadlineTimer 对象将被构造采用指定计时器 type .
此构造函数可以用于 C++ 14 的用户定义时间文字,譬如在:
using namespace std::chrono_literals; QDeadlineTimer deadline(250ms);
For optimization purposes, if remaining is zero or negative, 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 QDeadlineTimer::current () and add time to it.
另请参阅 hasExpired (), isForever (), remainingTime (),和 setRemainingTime ().
[static noexcept]
QDeadlineTimer
QDeadlineTimer::
addNSecs
(
QDeadlineTimer
dt
,
qint64
nsecs
)
返回 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.
[static noexcept]
QDeadlineTimer
QDeadlineTimer::
current
(
Qt::TimerType
timerType
= Qt::CoarseTimer)
返回 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 () 函数。
The QDeadlineTimer object will be constructed with the specified timerType .
[noexcept]
qint64
QDeadlineTimer::
deadline
() const
Returns the absolute time point for the deadline stored in QDeadlineTimer object, calculated in milliseconds relative to the reference clock, the same as QElapsedTimer::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 QDeadlineTimer::current () 或 QElapsedTimer::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.
另请参阅 remainingTime (), deadlineNSecs (),和 setDeadline ().
[noexcept]
qint64
QDeadlineTimer::
deadlineNSecs
() const
Returns the absolute time point for the deadline stored in QDeadlineTimer object, calculated in nanoseconds relative to the reference clock, the same as QElapsedTimer::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 QDeadlineTimer::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.
另请参阅 remainingTime () and deadlineNSecs().
[noexcept]
bool
QDeadlineTimer::
hasExpired
() const
返回 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.
另请参阅 isForever () 和 remainingTime ().
[constexpr noexcept]
bool
QDeadlineTimer::
isForever
() const
返回 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 ().
[noexcept]
qint64
QDeadlineTimer::
remainingTime
() const
返回剩余时间在此 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());
另请参阅 setRemainingTime (), remainingTimeNSecs (), isForever (),和 hasExpired ().
[noexcept]
std::chrono::nanoseconds
QDeadlineTimer::
remainingTimeAsDuration
() const
返回截止日期之前的剩余时间。
[noexcept]
qint64
QDeadlineTimer::
remainingTimeNSecs
() const
返回剩余时间在此 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.
另请参阅 remainingTime (), isForever (),和 hasExpired ().
[noexcept]
void
QDeadlineTimer::
setDeadline
(
qint64
msecs
,
Qt::TimerType
timerType
= Qt::CoarseTimer)
设置截止日期为此 QDeadlineTimer object to be the msecs absolute time point, counted in milliseconds since the reference clock (the same as QElapsedTimer::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.
另请参阅 setPreciseDeadline (), deadline (), deadlineNSecs (),和 setRemainingTime ().
设置此
QDeadlineTimer
to the deadline marked by
deadline
time point, converting from the clock source
Clock
to Qt's internal clock source (see
QElapsedTimer::clockType
()).
若
deadline
is in the past, this
QDeadlineTimer
object is set to expired, whereas if
deadline
等于
Duration::max()
, then this object is set to never expire.
The timer type for this QDeadlineTimer object will be set to the specified type .
另请参阅 hasExpired (), isForever (),和 remainingTime ().
[noexcept]
void
QDeadlineTimer::
setPreciseDeadline
(
qint64
secs
,
qint64
nsecs
= 0,
Qt::TimerType
timerType
= Qt::CoarseTimer)
设置截止日期为此 QDeadlineTimer object to be secs seconds and nsecs nanoseconds since the reference clock epoch (the same as QElapsedTimer::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.
另请参阅 setDeadline (), deadline (), deadlineNSecs (),和 setRemainingTime ().
[noexcept]
void
QDeadlineTimer::
setPreciseRemainingTime
(
qint64
secs
,
qint64
nsecs
= 0,
Qt::TimerType
timerType
= Qt::CoarseTimer)
设置剩余时间为此 QDeadlineTimer 对象到 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 QDeadlineTimer::current () and add time to it.
The timer type for this QDeadlineTimer object will be set to the specified timerType .
注意: Prior to Qt 6.6, the only condition that caused the timer to never expire was when secs was -1.
另请参阅 setRemainingTime (), hasExpired (), isForever (),和 remainingTime ().
[noexcept]
void
QDeadlineTimer::
setRemainingTime
(
qint64
msecs
,
Qt::TimerType
timerType
= Qt::CoarseTimer)
设置剩余时间为此 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 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 QDeadlineTimer::current () and add time to it.
The timer type for this QDeadlineTimer object will be set to the specified timerType .
注意: Prior to Qt 6.6, the only value that caused the timer to never expire was -1.
另请参阅 setPreciseRemainingTime (), hasExpired (), isForever (),和 remainingTime ().
这是重载函数。
设置剩余时间为此
QDeadlineTimer
对象到
remaining
。若
remaining
is zero or negative, this
QDeadlineTimer
object will be mark as expired, whereas if
remaining
等于
duration::max()
, the object will be set to never expire.
The timer type for this QDeadlineTimer object will be set to the specified type .
This function can be used with C++14's user-defined literals for time, such as in:
using namespace std::chrono_literals; deadline.setRemainingTime(250ms);
注意: Qt 检测必要 C++ 14 编译器支持,通过特征测试推荐从 C++ 委员会标准文档 6 .
另请参阅 setDeadline (), remainingTime (), hasExpired (),和 isForever ().
Changes the timer type for this object to timerType .
The behavior for each possible value of timerType is operating-system dependent. Qt::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 Qt::CoarseTimer and Qt::VeryCoarseTimer .
另请参阅 timerType () 和 Qt::TimerType .
[noexcept]
void
QDeadlineTimer::
swap
(
QDeadlineTimer
&
other
)
Swaps this deadline timer with the other deadline timer.
[noexcept]
Qt::TimerType
QDeadlineTimer::
timerType
() const
Returns the timer type is active for this object.
另请参阅 setTimerType ().
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 ().
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 ().
赋值 deadline_ 到此截止日期计时器。
Sets this deadline timer to the remaining time.
[noexcept]
bool
operator!=
(
QDeadlineTimer
d1
,
QDeadlineTimer
d2
)
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();
注意: 比较 QDeadlineTimer objects with different timer types is not supported and may result in unpredictable behavior.
返回 QDeadlineTimer object whose deadline is msecs later than the deadline stored in dt 。若 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 ().
返回 QDeadlineTimer object whose deadline is msecs later than the deadline stored in dt 。若 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 ().
返回 QDeadlineTimer object whose deadline is msecs before the deadline stored in dt 。若 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 ().
[noexcept]
bool
operator<
(
QDeadlineTimer
d1
,
QDeadlineTimer
d2
)
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();
注意: 比较 QDeadlineTimer objects with different timer types is not supported and may result in unpredictable behavior.
[noexcept]
bool
operator<=
(
QDeadlineTimer
d1
,
QDeadlineTimer
d2
)
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();
注意: 比较 QDeadlineTimer objects with different timer types is not supported and may result in unpredictable behavior.
[noexcept]
bool
operator==
(
QDeadlineTimer
d1
,
QDeadlineTimer
d2
)
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();
注意: 比较 QDeadlineTimer objects with different timer types is not supported and may result in unpredictable behavior.
[noexcept]
bool
operator>
(
QDeadlineTimer
d1
,
QDeadlineTimer
d2
)
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();
注意: 比较 QDeadlineTimer objects with different timer types is not supported and may result in unpredictable behavior.
[noexcept]
bool
operator>=
(
QDeadlineTimer
d1
,
QDeadlineTimer
d2
)
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();
注意: 比较 QDeadlineTimer objects with different timer types is not supported and may result in unpredictable behavior.