QDeadlineTimer 類標記未來最後期限。 更多...
| 頭: |
#include <QDeadlineTimer>
|
| CMake: |
find_package(Qt6 REQUIRED COMPONENTS Core)
target_link_libraries(mytarget PRIVATE Qt6::Core)
|
| qmake: |
QT += core
|
此類 強烈可比較 .
注意: 此類的所有函數 可重入 .
| 枚舉類 | 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::duration<Rep, Period> remaining , Qt::TimerType type = Qt::CoarseTimer) | |
| QDeadlineTimer (std::chrono::time_point<Clock, Duration> deadline , 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::duration<Rep, Period> remaining ) |
| QDeadlineTimer & | operator= (std::chrono::time_point<Clock, Duration> deadline_ ) |
| QDeadlineTimer | addNSecs (QDeadlineTimer dt , qint64 nsecs ) |
| QDeadlineTimer | current (Qt::TimerType timerType = Qt::CoarseTimer) |
| bool | operator!= (const QDeadlineTimer & lhs , const QDeadlineTimer & rhs ) |
| QDeadlineTimer | operator+ (QDeadlineTimer dt , qint64 msecs ) |
| QDeadlineTimer | operator+ (qint64 msecs , QDeadlineTimer dt ) |
| QDeadlineTimer | operator- (QDeadlineTimer dt , qint64 msecs ) |
| bool | operator< (const QDeadlineTimer & lhs , const QDeadlineTimer & rhs ) |
| bool | operator<= (const QDeadlineTimer & lhs , const QDeadlineTimer & rhs ) |
| bool | operator== (const QDeadlineTimer & lhs , const QDeadlineTimer & rhs ) |
| bool | operator> (const QDeadlineTimer & lhs , const QDeadlineTimer & rhs ) |
| bool | operator>= (const QDeadlineTimer & lhs , const QDeadlineTimer & rhs ) |
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 and QChronoTimer , 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 such as:
QDeadlineTimer deadline(30s);
device->waitForReadyRead(deadline);
if (deadline.remainingTime<std::chrono::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:
auto now = std::chrono::steady_clock::now();
QDeadlineTimer deadline(now + 1s);
Q_ASSERT(deadline == now + 1s);
另請參閱 QTime , QChronoTimer , 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 ().
構造 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 .
This constructor can be used with C++14's user-defined literals for time , such as in:
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 ().
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 ().
[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:
deadline.setRemainingTime(250ms);
另請參閱 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 other 。此操作很快且從不失敗。
[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 ().
Sets this deadline timer to the remaining time.
賦值 deadline_ 到此截止日期計時器。
[noexcept]
bool
operator!=
(const
QDeadlineTimer
&
lhs
, const
QDeadlineTimer
&
rhs
)
Returns true if the deadline on lhs and the deadline in rhs are different, false otherwise. The timer type used to create the two deadlines is ignored. This function is equivalent to:
return lhs.deadlineNSecs() != rhs.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<
(const
QDeadlineTimer
&
lhs
, const
QDeadlineTimer
&
rhs
)
Returns true if the deadline on lhs is earlier than the deadline in rhs , false otherwise. The timer type used to create the two deadlines is ignored. This function is equivalent to:
return lhs.deadlineNSecs() < rhs.deadlineNSecs();
注意: 比較 QDeadlineTimer objects with different timer types is not supported and may result in unpredictable behavior.
[noexcept]
bool
operator<=
(const
QDeadlineTimer
&
lhs
, const
QDeadlineTimer
&
rhs
)
Returns true if the deadline on lhs is earlier than or the same as the deadline in rhs , false otherwise. The timer type used to create the two deadlines is ignored. This function is equivalent to:
return lhs.deadlineNSecs() <= rhs.deadlineNSecs();
注意: 比較 QDeadlineTimer objects with different timer types is not supported and may result in unpredictable behavior.
[noexcept]
bool
operator==
(const
QDeadlineTimer
&
lhs
, const
QDeadlineTimer
&
rhs
)
Returns true if the deadline on lhs and the deadline in rhs are 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();
注意: 比較 QDeadlineTimer objects with different timer types is not supported and may result in unpredictable behavior.
[noexcept]
bool
operator>
(const
QDeadlineTimer
&
lhs
, const
QDeadlineTimer
&
rhs
)
Returns true if the deadline on lhs is later than the deadline in rhs , false otherwise. The timer type used to create the two deadlines is ignored. This function is equivalent to:
return lhs.deadlineNSecs() > rhs.deadlineNSecs();
注意: 比較 QDeadlineTimer objects with different timer types is not supported and may result in unpredictable behavior.
[noexcept]
bool
operator>=
(const
QDeadlineTimer
&
lhs
, const
QDeadlineTimer
&
rhs
)
Returns true if the deadline on lhs is later than or the same as the deadline in rhs , false otherwise. The timer type used to create the two deadlines is ignored. This function is equivalent to:
return lhs.deadlineNSecs() >= rhs.deadlineNSecs();
注意: 比較 QDeadlineTimer objects with different timer types is not supported and may result in unpredictable behavior.