Qt 6.x
The Qt SDK
Loading...
Searching...
No Matches
QDeadlineTimer Class Reference

\inmodule QtCore More...

#include <qdeadlinetimer.h>

+ Collaboration diagram for QDeadlineTimer:

Public Types

enum class  ForeverConstant { Forever }
 \value Forever Used when creating a QDeadlineTimer to indicate the deadline should not expire More...
 

Public Member Functions

constexpr QDeadlineTimer (Qt::TimerType type_=Qt::CoarseTimer) noexcept
 Constructs an expired QDeadlineTimer object.
 
constexpr QDeadlineTimer (ForeverConstant, Qt::TimerType type_=Qt::CoarseTimer) noexcept
 QDeadlineTimer objects created with ForeverConstant never expire.
 
 QDeadlineTimer (qint64 msecs, Qt::TimerType type=Qt::CoarseTimer) noexcept
 Constructs a QDeadlineTimer object with an expiry time of msecs msecs from the moment of the creation of this object, if msecs is positive.
 
void swap (QDeadlineTimer &other) noexcept
 Swaps this deadline timer with the other deadline timer.
 
constexpr bool isForever () const noexcept
 Returns true if this QDeadlineTimer object never expires, false otherwise.
 
bool hasExpired () const noexcept
 Returns true if this QDeadlineTimer object has expired, false if there remains time left.
 
Qt::TimerType timerType () const noexcept
 Returns the timer type is active for this object.
 
void setTimerType (Qt::TimerType type)
 Changes the timer type for this object to timerType.
 
qint64 remainingTime () const noexcept
 Returns the remaining time in this QDeadlineTimer object in milliseconds.
 
qint64 remainingTimeNSecs () const noexcept
 Returns the remaining time in this QDeadlineTimer object in nanoseconds.
 
void setRemainingTime (qint64 msecs, Qt::TimerType type=Qt::CoarseTimer) noexcept
 Sets the remaining time for this QDeadlineTimer object to msecs milliseconds from now, if msecs has a positive value.
 
void setPreciseRemainingTime (qint64 secs, qint64 nsecs=0, Qt::TimerType type=Qt::CoarseTimer) noexcept
 Sets the remaining time for this QDeadlineTimer object to secs seconds plus nsecs nanoseconds from now, if secs has a positive value.
 
qint64 deadline () const noexcept Q_DECL_PURE_FUNCTION
 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().
 
qint64 deadlineNSecs () const noexcept Q_DECL_PURE_FUNCTION
 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().
 
void setDeadline (qint64 msecs, Qt::TimerType timerType=Qt::CoarseTimer) noexcept
 Sets the deadline for this 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.
 
void setPreciseDeadline (qint64 secs, qint64 nsecs=0, Qt::TimerType type=Qt::CoarseTimer) noexcept
 Sets the deadline for this 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.
 
QDeadlineTimeroperator+= (qint64 msecs)
 Extends this QDeadlineTimer object by msecs milliseconds and returns itself.
 
QDeadlineTimeroperator-= (qint64 msecs)
 Shortens this QDeadlineTimer object by msecs milliseconds and returns itself.
 
template<class Clock , class Duration = typename Clock::duration>
 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()).
 
template<class Clock , class Duration = typename Clock::duration>
QDeadlineTimeroperator= (std::chrono::time_point< Clock, Duration > deadline_)
 Assigns deadline_ to this deadline timer.
 
template<class Clock , class Duration = typename Clock::duration>
void setDeadline (std::chrono::time_point< Clock, Duration > tp, Qt::TimerType type_=Qt::CoarseTimer)
 Sets this QDeadlineTimer to the deadline marked by deadline time point, converting from the clock source {Clock} to Qt's internal clock source (see QElapsedTimer::clockType()).
 
template<class Clock , class Duration = typename Clock::duration>
std::chrono::time_point< Clock, Duration > deadline () const
 
template<class Rep , class Period >
 QDeadlineTimer (std::chrono::duration< Rep, Period > remaining, Qt::TimerType type_=Qt::CoarseTimer)
 Constructs a QDeadlineTimer object with a remaining time of remaining.
 
template<class Rep , class Period >
QDeadlineTimeroperator= (std::chrono::duration< Rep, Period > remaining)
 Sets this deadline timer to the remaining time.
 
template<class Rep , class Period >
void setRemainingTime (std::chrono::duration< Rep, Period > remaining, Qt::TimerType type_=Qt::CoarseTimer)
 This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.
 
std::chrono::nanoseconds remainingTimeAsDuration () const noexcept
 Returns the time remaining before the deadline.
 

Static Public Member Functions

static QDeadlineTimer addNSecs (QDeadlineTimer dt, qint64 nsecs) noexcept Q_DECL_PURE_FUNCTION
 Returns a QDeadlineTimer object whose deadline is extended from dt's deadline by nsecs nanoseconds.
 
static QDeadlineTimer current (Qt::TimerType timerType=Qt::CoarseTimer) noexcept
 Returns a QDeadlineTimer that is expired but is guaranteed to contain the current time.
 

Static Public Attributes

static constexpr ForeverConstant Forever = ForeverConstant::Forever
 

Friends

bool operator== (QDeadlineTimer d1, QDeadlineTimer d2) noexcept
 Returns true if the deadline on d1 and the deadline in d2 are the same, false otherwise.
 
bool operator!= (QDeadlineTimer d1, QDeadlineTimer d2) noexcept
 Returns true if the deadline on d1 and the deadline in d2 are different, false otherwise.
 
bool operator< (QDeadlineTimer d1, QDeadlineTimer d2) noexcept
 Returns true if the deadline on d1 is earlier than the deadline in d2, false otherwise.
 
bool operator<= (QDeadlineTimer d1, QDeadlineTimer d2) noexcept
 Returns true if the deadline on d1 is earlier than or the same as the deadline in d2, false otherwise.
 
bool operator> (QDeadlineTimer d1, QDeadlineTimer d2) noexcept
 Returns true if the deadline on d1 is later than the deadline in d2, false otherwise.
 
bool operator>= (QDeadlineTimer d1, QDeadlineTimer d2) noexcept
 Returns true if the deadline on d1 is later than or the same as the deadline in d2, false otherwise.
 
Q_CORE_EXPORT QDeadlineTimer operator+ (QDeadlineTimer dt, qint64 msecs)
 Returns a QDeadlineTimer object whose deadline is msecs later than the deadline stored in dt.
 
QDeadlineTimer operator+ (qint64 msecs, QDeadlineTimer dt)
 Returns a QDeadlineTimer object whose deadline is msecs later than the deadline stored in dt.
 
QDeadlineTimer operator- (QDeadlineTimer dt, qint64 msecs)
 Returns a QDeadlineTimer object whose deadline is msecs before the deadline stored in dt.
 
qint64 operator- (QDeadlineTimer dt1, QDeadlineTimer dt2)
 
template<class Rep , class Period >
QDeadlineTimer operator+ (QDeadlineTimer dt, std::chrono::duration< Rep, Period > value)
 
template<class Rep , class Period >
QDeadlineTimer operator+ (std::chrono::duration< Rep, Period > value, QDeadlineTimer dt)
 
template<class Rep , class Period >
QDeadlineTimer operator+= (QDeadlineTimer &dt, std::chrono::duration< Rep, Period > value)
 

Detailed Description

\inmodule QtCore

The QDeadlineTimer class marks a deadline in the future.

Since
5.8

\reentrant

The QDeadlineTimer class is usually used to calculate future deadlines and verify whether the deadline has expired. QDeadlineTimer can also be used for deadlines without expiration ("forever"). It forms a counterpart to QElapsedTimer, which calculates how much time has elapsed since QElapsedTimer::start() was called.

QDeadlineTimer provides a more convenient API compared to QElapsedTimer::hasExpired().

The typical use-case for the class is to create a QDeadlineTimer before the operation in question is started, and then use remainingTime() or hasExpired() to determine whether to continue trying the operation. QDeadlineTimer objects can be passed to functions being called to execute this operation so they know how long to still operate.

void executeOperation(int msecs)
{
do {
if (readFromDevice(deadline.remainingTime()))
break;
} while (!deadline.hasExpired());
}
\inmodule QtCore
qint64 deadline() const noexcept Q_DECL_PURE_FUNCTION
Returns the absolute time point for the deadline stored in QDeadlineTimer object, calculated in milli...
static bool readFromDevice(QIODevice *device, QJsonArray *allMetaObjects)
device waitForReadyRead(deadline)
void executeOperation(int msecs)
[0]

Many QDeadlineTimer functions deal with time out values, which all are measured in milliseconds. There are two special values, the same as many other Qt functions named {waitFor} or similar:

\list

  • 0: no time left, expired
  • -1: infinite time left, timer never expires \endlist

Definition at line 23 of file qdeadlinetimer.h.

Member Enumeration Documentation

◆ ForeverConstant

\value Forever Used when creating a QDeadlineTimer to indicate the deadline should not expire

Enumerator
Forever 

Definition at line 26 of file qdeadlinetimer.h.

Constructor & Destructor Documentation

◆ QDeadlineTimer() [1/5]

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

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

See also
hasExpired(), remainingTime(), Qt::TimerType, current()

Definition at line 29 of file qdeadlinetimer.h.

◆ QDeadlineTimer() [2/5]

QDeadlineTimer::QDeadlineTimer ( ForeverConstant  ,
Qt::TimerType  timerType = Qt::CoarseTimer 
)
inlineconstexprnoexcept

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.

See also
ForeverConstant, hasExpired(), isForever(), remainingTime(), timerType()

Definition at line 31 of file qdeadlinetimer.h.

◆ QDeadlineTimer() [3/5]

QDeadlineTimer::QDeadlineTimer ( qint64  msecs,
Qt::TimerType  type = Qt::CoarseTimer 
)
explicitnoexcept

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.

The 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 QDeadlineTimer::current() and add time to it.

Note
Prior to Qt 6.6, the only value that caused the timer to never expire was -1.
See also
hasExpired(), isForever(), remainingTime(), setRemainingTime()

Definition at line 185 of file qdeadlinetimer.cpp.

References setRemainingTime().

+ Here is the call graph for this function:

◆ QDeadlineTimer() [4/5]

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

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

If deadline is in the past, this QDeadlineTimer object is set to expired, whereas if deadline is equal to {Duration::max()}, then this object is set to never expire.

The QDeadlineTimer object will be constructed with the specified timer type.

See also
hasExpired(), isForever(), remainingTime(), setDeadline()

Definition at line 87 of file qdeadlinetimer.h.

◆ QDeadlineTimer() [5/5]

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

Constructs a QDeadlineTimer object with a remaining time of remaining.

If remaining is zero or negative, this QDeadlineTimer object will be mark as expired, whereas if remaining is equal to {duration::max()}, the object will be set to never expire.

The QDeadlineTimer object will be constructed with the specified timer type.

This constructor can be used with C++14's user-defined literals for time, such as in:

using namespace std::chrono_literals;

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.

See also
hasExpired(), isForever(), remainingTime(), setRemainingTime()

Definition at line 102 of file qdeadlinetimer.h.

References setRemainingTime().

+ Here is the call graph for this function:

Member Function Documentation

◆ addNSecs()

QDeadlineTimer QDeadlineTimer::addNSecs ( QDeadlineTimer  dt,
qint64  nsecs 
)
staticnoexcept

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

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

Definition at line 560 of file qdeadlinetimer.cpp.

References add_saturate().

+ Here is the call graph for this function:

◆ current()

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

Returns a 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() function.

The QDeadlineTimer object will be constructed with the specified timerType.

Definition at line 578 of file qdeadlinetimer.cpp.

Referenced by fallback_fill(), getticks(), hasExpired(), and if().

+ Here is the caller graph for this function:

◆ deadline() [1/2]

template<class Clock , class Duration >
std::chrono::time_point< Clock, Duration > QDeadlineTimer::deadline

Definition at line 151 of file qdeadlinetimer.h.

References deadlineNSecs().

+ Here is the call graph for this function:

◆ deadline() [2/2]

qint64 QDeadlineTimer::deadline ( ) const
noexcept

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.

If this 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() or QElapsedTimer::msecsSinceReference(), as in the following example:

if (realTimeLeft != (std::numeric_limits<qint64>::max)()) {
// or:
//QElapsedTimer timer;
//timer.start();
//realTimeLeft -= timer.msecsSinceReference();
}
static QDeadlineTimer current(Qt::TimerType timerType=Qt::CoarseTimer) noexcept
Returns a QDeadlineTimer that is expired but is guaranteed to contain the current time.
long long qint64
Definition qtypes.h:55
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
remainingTime(), deadlineNSecs(), setDeadline()

Definition at line 470 of file qdeadlinetimer.cpp.

References isForever().

Referenced by fallback_fill(), QtLinuxFutex::futexWait(), if(), and qt_abstime_for_timeout().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ deadlineNSecs()

qint64 QDeadlineTimer::deadlineNSecs ( ) const
noexcept

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.

If this 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())
qint64 deadlineNSecs() const noexcept Q_DECL_PURE_FUNCTION
Returns the absolute time point for the deadline stored in QDeadlineTimer object, calculated in nanos...
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
remainingTime(), deadlineNSecs()

Definition at line 502 of file qdeadlinetimer.cpp.

References isForever().

Referenced by getticks().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ hasExpired()

bool QDeadlineTimer::hasExpired ( ) const
noexcept

Returns true if this 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 \l {ForeverConstant} never expire and this function always returns false for them.

See also
isForever(), remainingTime()

Definition at line 358 of file qdeadlinetimer.cpp.

References current(), isForever(), and timerType().

Referenced by executeOperation(), QEventLoop::processEvents(), QCoreApplication::processEvents(), QTest::qWait(), QQmlInstantiationInterrupt::shouldInterrupt(), QSingleShotTimer::startTimerForReceiver(), QNetworkAccessCache::timerEvent(), and QWaitConditionPrivate::wait().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ isForever()

bool QDeadlineTimer::isForever ( ) const
inlineconstexprnoexcept

Returns true if this QDeadlineTimer object never expires, false otherwise.

For timers that never expire, remainingTime() always returns -1 and deadline() returns the maximum value.

See also
ForeverConstant, hasExpired(), remainingTime()

Definition at line 38 of file qdeadlinetimer.h.

Referenced by deadline(), deadlineNSecs(), hasExpired(), QTest::qWait(), remainingTime(), remainingTimeNSecs(), QWaitConditionPrivate::wait(), and QWaitConditionPrivate::wait().

+ Here is the caller graph for this function:

◆ operator+=()

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

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

Definition at line 81 of file qdeadlinetimer.h.

◆ operator-=()

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

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

Definition at line 83 of file qdeadlinetimer.h.

◆ operator=() [1/2]

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

Sets this deadline timer to the remaining time.

Definition at line 107 of file qdeadlinetimer.h.

References setRemainingTime().

+ Here is the call graph for this function:

◆ operator=() [2/2]

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

Assigns deadline_ to this deadline timer.

Definition at line 91 of file qdeadlinetimer.h.

◆ remainingTime()

qint64 QDeadlineTimer::remainingTime ( ) const
noexcept

Returns the remaining time in this 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 \l QIODevice waitFor functions or the timed lock functions in \l QMutex, \l QWaitCondition, \l QSemaphore, or \l QReadWriteLock. For example:

mutex.tryLock(deadline.remainingTime());
bool tryLock(int timeout=0) noexcept
Attempts to lock the mutex.
Definition qmutex.h:291
QMutex mutex
[2]
See also
remainingTimeNSecs(), isForever(), hasExpired()

Definition at line 407 of file qdeadlinetimer.cpp.

References isForever(), and remainingTimeNSecs().

Referenced by executeOperation(), QtWindowsFutex::futexWait(), QSingleShotTimer::startTimerForReceiver(), and QWaitConditionPrivate::wait().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ remainingTimeAsDuration()

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

Returns the time remaining before the deadline.

Definition at line 120 of file qdeadlinetimer.h.

Referenced by QTest::qWait().

+ Here is the caller graph for this function:

◆ remainingTimeNSecs()

qint64 QDeadlineTimer::remainingTimeNSecs ( ) const
noexcept

Returns the remaining time in this 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.

See also
remainingTime(), isForever(), hasExpired()

Definition at line 424 of file qdeadlinetimer.cpp.

References isForever().

Referenced by remainingTime().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ setDeadline() [1/2]

void QDeadlineTimer::setDeadline ( qint64  msecs,
Qt::TimerType  timerType = Qt::CoarseTimer 
)
noexcept

Sets the deadline for this 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.

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

See also
setPreciseDeadline(), deadline(), deadlineNSecs(), setRemainingTime()

Definition at line 523 of file qdeadlinetimer.cpp.

References add_saturate().

+ Here is the call graph for this function:

◆ setDeadline() [2/2]

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

Sets this QDeadlineTimer to the deadline marked by deadline time point, converting from the clock source {Clock} to Qt's internal clock source (see QElapsedTimer::clockType()).

If deadline is in the past, this QDeadlineTimer object is set to expired, whereas if deadline is equal to {Duration::max()}, then this object is set to never expire.

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

See also
hasExpired(), isForever(), remainingTime(),

Definition at line 164 of file qdeadlinetimer.h.

◆ setPreciseDeadline()

void QDeadlineTimer::setPreciseDeadline ( qint64  secs,
qint64  nsecs = 0,
Qt::TimerType  timerType = Qt::CoarseTimer 
)
noexcept

Sets the deadline for this 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.

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

See also
setDeadline(), deadline(), deadlineNSecs(), setRemainingTime()

Definition at line 546 of file qdeadlinetimer.cpp.

References add_saturate().

+ Here is the call graph for this function:

◆ setPreciseRemainingTime()

void QDeadlineTimer::setPreciseRemainingTime ( qint64  secs,
qint64  nsecs = 0,
Qt::TimerType  timerType = Qt::CoarseTimer 
)
noexcept

Sets the remaining time for this QDeadlineTimer object to 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.

Note
Prior to Qt 6.6, the only condition that caused the timer to never expire was when secs was -1.
See also
setRemainingTime(), hasExpired(), isForever(), remainingTime()

Definition at line 302 of file qdeadlinetimer.cpp.

References add_saturate().

+ Here is the call graph for this function:

◆ setRemainingTime() [1/2]

void QDeadlineTimer::setRemainingTime ( qint64  msecs,
Qt::TimerType  timerType = Qt::CoarseTimer 
)
noexcept

Sets the remaining time for this QDeadlineTimer object to 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.

Note
Prior to Qt 6.6, the only value that caused the timer to never expire was -1.
See also
setPreciseRemainingTime(), hasExpired(), isForever(), remainingTime()

Definition at line 266 of file qdeadlinetimer.cpp.

References add_saturate().

+ Here is the call graph for this function:

◆ setRemainingTime() [2/2]

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

This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.

Sets the remaining time for this QDeadlineTimer object to remaining. If remaining is zero or negative, this QDeadlineTimer object will be mark as expired, whereas if remaining is equal to {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);
Note
Qt detects the necessary C++14 compiler support by way of the feature test recommendations from \l{https://isocpp.org/std/standing-documents/sd-6-sg10-feature-test-recommendations} {C++ Committee's Standing Document 6}.
See also
setDeadline(), remainingTime(), hasExpired(), isForever()

Definition at line 111 of file qdeadlinetimer.h.

◆ setTimerType()

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.

See also
Qt::TimerType

Definition at line 386 of file qdeadlinetimer.cpp.

References timerType().

+ Here is the call graph for this function:

◆ swap()

void QDeadlineTimer::swap ( QDeadlineTimer other)
inlinenoexcept

Swaps this deadline timer with the other deadline timer.

Definition at line 35 of file qdeadlinetimer.h.

References other().

+ Here is the call graph for this function:

◆ timerType()

Qt::TimerType QDeadlineTimer::timerType ( ) const
inlinenoexcept

Returns the timer type is active for this object.

See also
setTimerType()

Definition at line 42 of file qdeadlinetimer.h.

Referenced by hasExpired(), and setTimerType().

+ Here is the caller graph for this function:

Friends And Related Symbol Documentation

◆ operator!=

bool QDeadlineTimer::operator!= ( QDeadlineTimer  d1,
QDeadlineTimer  d2 
)
friend

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();
QDate d1(1995, 5, 17)
[0]
QDate d2(1995, 5, 20)
Note
comparing QDeadlineTimer objects with different timer types is not supported and may result in unpredictable behavior.

Definition at line 63 of file qdeadlinetimer.h.

◆ operator+ [1/4]

QDeadlineTimer QDeadlineTimer::operator+ ( QDeadlineTimer  dt,
qint64  msecs 
)
friend

Returns a QDeadlineTimer object whose deadline is msecs later than the deadline stored in dt.

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

Definition at line 678 of file qdeadlinetimer.cpp.

◆ operator+ [2/4]

template<class Rep , class Period >
QDeadlineTimer operator+ ( QDeadlineTimer  dt,
std::chrono::duration< Rep, Period >  value 
)
friend

Definition at line 131 of file qdeadlinetimer.h.

◆ operator+ [3/4]

QDeadlineTimer QDeadlineTimer::operator+ ( qint64  msecs,
QDeadlineTimer  dt 
)
friend

Returns a QDeadlineTimer object whose deadline is msecs later than the deadline stored in dt.

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

Definition at line 75 of file qdeadlinetimer.h.

◆ operator+ [4/4]

template<class Rep , class Period >
QDeadlineTimer operator+ ( std::chrono::duration< Rep, Period >  value,
QDeadlineTimer  dt 
)
friend

Definition at line 134 of file qdeadlinetimer.h.

◆ operator+=

template<class Rep , class Period >
QDeadlineTimer operator+= ( QDeadlineTimer dt,
std::chrono::duration< Rep, Period >  value 
)
friend

Definition at line 137 of file qdeadlinetimer.h.

◆ operator- [1/2]

QDeadlineTimer QDeadlineTimer::operator- ( QDeadlineTimer  dt,
qint64  msecs 
)
friend

Returns a QDeadlineTimer object whose deadline is msecs before the deadline stored in dt.

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

Definition at line 77 of file qdeadlinetimer.h.

◆ operator- [2/2]

qint64 operator- ( QDeadlineTimer  dt1,
QDeadlineTimer  dt2 
)
friend

Definition at line 79 of file qdeadlinetimer.h.

◆ operator<

bool QDeadlineTimer::operator< ( QDeadlineTimer  d1,
QDeadlineTimer  d2 
)
friend

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();
Note
comparing QDeadlineTimer objects with different timer types is not supported and may result in unpredictable behavior.

Definition at line 65 of file qdeadlinetimer.h.

◆ operator<=

bool QDeadlineTimer::operator<= ( QDeadlineTimer  d1,
QDeadlineTimer  d2 
)
friend

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();
Note
comparing QDeadlineTimer objects with different timer types is not supported and may result in unpredictable behavior.

Definition at line 67 of file qdeadlinetimer.h.

◆ operator==

bool QDeadlineTimer::operator== ( QDeadlineTimer  d1,
QDeadlineTimer  d2 
)
friend

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();
Note
comparing QDeadlineTimer objects with different timer types is not supported and may result in unpredictable behavior.

Definition at line 61 of file qdeadlinetimer.h.

◆ operator>

bool QDeadlineTimer::operator> ( QDeadlineTimer  d1,
QDeadlineTimer  d2 
)
friend

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();
Note
comparing QDeadlineTimer objects with different timer types is not supported and may result in unpredictable behavior.

Definition at line 69 of file qdeadlinetimer.h.

◆ operator>=

bool QDeadlineTimer::operator>= ( QDeadlineTimer  d1,
QDeadlineTimer  d2 
)
friend

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();
Note
comparing QDeadlineTimer objects with different timer types is not supported and may result in unpredictable behavior.

Definition at line 71 of file qdeadlinetimer.h.

Member Data Documentation

◆ Forever

constexpr ForeverConstant QDeadlineTimer::Forever = ForeverConstant::Forever
staticconstexpr

The documentation for this class was generated from the following files: