QDeadlineTimer 類

QDeadlineTimer 類標記未來最後期限。 更多...

頭: #include <QDeadlineTimer>
qmake: QT += core
Since: Qt 5.8

該類在 Qt 5.8 引入。

注意: 此類的所有函數 可重入 .

公共類型

enum ForeverConstant { Forever }

公共函數

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)
QDeadlineTimer (qint64 msecs , Qt::TimerType type = Qt::CoarseTimer)
QDeadlineTimer ( QDeadlineTimer::ForeverConstant , Qt::TimerType timerType = Qt::CoarseTimer)
QDeadlineTimer (Qt::TimerType timerType = 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 或類似:

  • 0:沒有剩餘時間,過期
  • -1:無限剩餘時間,計時器從不過期

參考時鍾

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 .

成員類型文檔編製

enum QDeadlineTimer:: ForeverConstant

常量 描述
QDeadlineTimer::Forever 0 使用當創建 QDeadlineTimer 以指示截止日期不應該過期

成員函數文檔編製

template <typename Rep, typename Period> QDeadlineTimer:: QDeadlineTimer ( std::chrono::duration < Rep , Period > remaining , Qt::TimerType type = Qt::CoarseTimer)

構造 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 ().

template <typename Clock, typename Duration> QDeadlineTimer:: QDeadlineTimer ( std::chrono::time_point < Clock , Duration > deadline , Qt::TimerType type = Qt::CoarseTimer)

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:: 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 -1, 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.

另請參閱 hasExpired (), isForever (), remainingTime (),和 setRemainingTime ().

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 ().

QDeadlineTimer:: QDeadlineTimer ( Qt::TimerType timerType = Qt::CoarseTimer)

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 QDeadlineTimer::current ().

另請參閱 hasExpired (), remainingTime (), Qt::TimerType ,和 current ().

[static] 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] 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 .

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 ().

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().

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 ().

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 ().

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 ().

std::chrono::nanoseconds QDeadlineTimer:: remainingTimeAsDuration () const

返迴截止日期之前的剩餘時間。

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 ().

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 ().

template <typename Clock, typename Duration> void QDeadlineTimer:: setDeadline ( std::chrono::time_point < Clock , Duration > deadline , Qt::TimerType type = Qt::CoarseTimer)

設置此 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 ().

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 ().

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 -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 .

另請參閱 setRemainingTime (), hasExpired (), isForever (),和 remainingTime ().

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 value of -1 will set it to never expire.

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

另請參閱 setPreciseRemainingTime (), hasExpired (), isForever (),和 remainingTime ().

template <typename Rep, typename Period> void QDeadlineTimer:: setRemainingTime ( std::chrono::duration < Rep , Period > remaining , Qt::TimerType type = Qt::CoarseTimer)

這是重載函數。

設置剩餘時間為此 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 ().

void QDeadlineTimer:: setTimerType ( Qt::TimerType timerType )

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 .

void QDeadlineTimer:: swap ( QDeadlineTimer & other )

Swaps this deadline timer with the other deadline timer.

Qt::TimerType QDeadlineTimer:: timerType () const

Returns the timer type is active for this object.

另請參閱 setTimerType ().

QDeadlineTimer &QDeadlineTimer:: operator+= ( qint64 msecs )

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 ().

QDeadlineTimer &QDeadlineTimer:: operator-= ( qint64 msecs )

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 ().

template <typename Clock, typename Duration> QDeadlineTimer &QDeadlineTimer:: operator= ( std::chrono::time_point < Clock , Duration > deadline_ )

賦值 deadline_ 到此截止日期計時器。

template <typename Rep, typename Period> QDeadlineTimer &QDeadlineTimer:: operator= ( std::chrono::duration < Rep , Period > remaining )

Sets this deadline timer to the remaining time.

相關非成員

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 operator+ ( QDeadlineTimer dt , qint64 msecs )

返迴 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 operator+ ( qint64 msecs , QDeadlineTimer dt )

返迴 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 operator- ( QDeadlineTimer dt , qint64 msecs )

返迴 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 ().

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.

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.

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.

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.

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.