QElapsedTimer Class
QElapsedTimer 类提供了一种快速计算经过时间的方法。更多
Header: | #include <QElapsedTimer> |
CMake.QElapsedTimer | find_package(Qt6 REQUIRED COMPONENTS Core) target_link_libraries(mytarget PRIVATE Qt6::Core) |
qmake: | QT += core |
该类具有很强的可比性。
注意:该类中的所有函数都是可重入的。
公共类型
enum | ClockType { SystemTime, MonotonicClock, TickCounter, MachAbsoluteTime, PerformanceCounter } |
Duration | |
TimePoint |
公共函数
QElapsedTimer() | |
(since 6.6) QElapsedTimer::Duration | durationElapsed() const |
(since 6.6) QElapsedTimer::Duration | durationTo(const QElapsedTimer &other) const |
qint64 | elapsed() const |
bool | hasExpired(qint64 timeout) const |
void | invalidate() |
bool | isValid() const |
qint64 | msecsSinceReference() const |
qint64 | msecsTo(const QElapsedTimer &other) const |
qint64 | nsecsElapsed() const |
qint64 | restart() |
qint64 | secsTo(const QElapsedTimer &other) const |
void | start() |
静态公共成员
QElapsedTimer::ClockType | clockType() |
bool | isMonotonic() |
相关非成员
bool | operator!=(const QElapsedTimer &lhs, const QElapsedTimer &rhs) |
bool | operator<(const QElapsedTimer &lhs, const QElapsedTimer &rhs) |
bool | operator==(const QElapsedTimer &lhs, const QElapsedTimer &rhs) |
详细说明
QElapsedTimer 类通常用于快速计算两个事件之间的时间间隔。它的 API 与QTime 相似,因此使用该类的代码可以快速移植到新类。
不过,与QTime 不同的是,QElapsedTimer 尽可能使用单调时钟。这意味着无法将 QElapsedTimer 对象转换为人类可读的时间。
该类的典型用例是确定慢速操作花费了多少时间。这种情况最简单的例子就是用于调试,如下面的示例:
QElapsedTimertimer; timer.start(); slowOperation1(); qDebug() << "The slow operation took" << timer.elapsed() << "milliseconds";
在本例中,定时器通过调用start() 启动,所用时间由elapsed() 函数计算。
在第一个操作完成后,经过的时间还可用于重新计算另一个操作的可用时间。当执行必须在一定时间内完成,但需要几个步骤时,这种方法非常有用。QIODevice 及其子类中的waitFor
类型函数就是这种需要的很好例子。在这种情况下,代码可以如下所示:
void executeSlowOperations(int timeout) { QElapsedTimer timer; timer.start(); slowOperation1(); int remainingTime = timeout - timer.elapsed(); if (remainingTime > 0) slowOperation2(remainingTime); }
另一种用例是在特定时间片内执行特定操作。为此,QElapsedTimer 提供了hasExpired() 方便函数,可用于确定某个毫秒数是否已经过去:
void executeOperationsForTime(int ms) { QElapsedTimer timer; timer.start(); while (!timer.hasExpired(ms)) slowOperation1(); }
在这种情况下,通常使用QDeadlineTimer 更为方便,因为它可以计算未来的超时时间,而不是跟踪已过去的时间。
参考时钟
QElapsedTimer 将在所有支持平台的平台中使用平台的单调参考时钟(请参阅QElapsedTimer::isMonotonic() )。这样做的另一个好处是,QElapsedTimer 不受时间调整的影响,如用户校正时间。此外,与QTime 不同,QElapsedTimer 不受时区设置变化(如夏令时)的影响。
另一方面,这意味着 QElapsedTimer 的值只能与使用相同参照的其他值进行比较。如果从 QElapsedTimer 对象(QElapsedTimer::msecsSinceReference())中提取自引用的时间并进行序列化,情况尤其如此。这些值绝对不能在网络上交换或保存到磁盘上,因为不知道接收数据的计算机节点是否与发送数据的计算机节点相同,也不知道它是否在此之后重新启动过。
不过,只要在同一台机器上运行的其他进程也使用相同的参考时钟,就可以与它们交换值。QElapsedTimer 将始终使用相同的时钟,因此与来自同一台机器上另一个进程的值进行比较是安全的。如果要与其他 API 生成的值进行比较,则应检查所用时钟是否与 QElapsedTimer 相同(请参阅QElapsedTimer::clockType() )。
另请参阅 QTime,QChronoTimer, 和QDeadlineTimer 。
成员类型文档
enum QElapsedTimer::ClockType
该枚举包含QElapsedTimer 可能使用的不同时钟类型。
QElapsedTimer 将始终在特定机器中使用相同的时钟类型,因此该值在程序生命周期内不会改变。提供该值是为了让 可以与其他非 Qt XML 实现一起使用,以保证使用的是相同的参考时钟。QElapsedTimer
常量 | 值 | 说明 |
---|---|---|
QElapsedTimer::SystemTime | 0 | 人类可读的系统时间。该时钟不是单调时钟。 |
QElapsedTimer::MonotonicClock | 1 | 系统单调时钟,通常存在于 Unix 系统中。该时钟为单调时钟。 |
QElapsedTimer::TickCounter | 2 | 不再使用。 |
QElapsedTimer::MachAbsoluteTime | 3 | 马赫内核的绝对时间(macOS 和 iOS)。该时钟为单调时钟。 |
QElapsedTimer::PerformanceCounter | 4 | Windows 提供的性能计数器。此时钟为单调时钟。 |
系统时间
系统时间时钟是纯粹的真实时间,以毫秒为单位,从 1970 年 1 月 1 日 0:00 UTC 开始计算。它相当于 C 和 POSIXtime
函数返回的值,并加上了毫秒。这种时钟类型目前只用于不支持单调时钟的 Unix 系统(见下文)。
这是QElapsedTimer 可以使用的唯一非单调时钟。
单调时钟
这是系统的单调时钟,以毫秒为单位,从过去的任意点开始计时。这种时钟类型用于支持 POSIX 单调时钟的 Unix 系统 (_POSIX_MONOTONIC_CLOCK
)。
马赫绝对时间
这种时钟类型基于马赫内核(如 macOS 上的马赫内核)提供的绝对时间。由于 macOS 和 iOS 也是 Unix 系统,可能支持 POSIX 单调时钟,其值与马赫绝对时间不同,因此将此时钟类型与 MonotonicClock 分开介绍。
此时钟为单调时钟。
性能计数器
该时钟使用 Windows 函数QueryPerformanceCounter
和QueryPerformanceFrequency
访问系统的性能计数器。
该时钟为单调时钟。
另请参阅 clockType() 和isMonotonic()。
[alias]
QElapsedTimer::Duration
std::chrono::nanoseconds
的同义词。
[alias]
QElapsedTimer::TimePoint
std::chrono::time_point<std::chrono::steady_clock, Duration>
的同义词。
成员函数文档
[constexpr noexcept]
QElapsedTimer::QElapsedTimer()
构造一个无效的 QElapsedTimer。定时器一旦启动就会生效。
[static noexcept]
QElapsedTimer::ClockType QElapsedTimer::clockType()
返回QElapsedTimer 实现使用的时钟类型。
自 Qt XML 6.6 起,QElapsedTimer 使用std::chrono::steady_clock
,因此时钟类型始终是MonotonicClock 。
另请参阅 isMonotonic() 。
[noexcept, since 6.6]
QElapsedTimer::Duration QElapsedTimer::durationElapsed() const
Returns astd::chrono::nanoseconds
with the time since thisQElapsedTimer was last started.
在无效的QElapsedTimer 上调用此函数将导致未定义的行为。
在不提供纳秒分辨率的平台上,返回的值将是可用的最佳估计值。
此函数在 Qt 6.6 中引入。
另请参阅 start()、restart()、hasExpired() 和invalidate()。
[noexcept, since 6.6]
QElapsedTimer::Duration QElapsedTimer::durationTo(const QElapsedTimer &other) const
以std::chrono::nanoseconds
的形式返回该QElapsedTimer 与other 之间的时间差。如果other 在该对象之前启动,返回值将为负数。如果在此之后启动,则返回值为正数。
如果此对象或other 已失效,则返回值未定义。
此函数在 Qt 6.6 中引入。
[noexcept]
qint64 QElapsedTimer::elapsed() const
返回QElapsedTimer 上次启动后的毫秒数。
在无效的QElapsedTimer 上调用此函数将导致未定义的行为。
另请参阅 start()、restart()、hasExpired()、isValid() 和invalidate()。
[noexcept]
bool QElapsedTimer::hasExpired(qint64 timeout) const
如果elapsed() 超过给定的timeout ,则返回true
,否则返回false
。
负数timeout 会被解释为无限,因此在这种情况下会返回false
。否则,返回值等同于elapsed() > timeout
。您可以通过比较durationElapsed() 和超时持续时间,对持续时间做同样的处理。
另请参阅 elapsed() 和QDeadlineTimer 。
[noexcept]
void QElapsedTimer::invalidate()
将QElapsedTimer 对象标记为无效。
可以使用isValid() 检查无效对象。计算无效数据后的计时时间是未定义的,可能会产生奇怪的结果。
另请参阅 isValid()、start() 和restart()。
[static noexcept]
bool QElapsedTimer::isMonotonic()
如果是单调时钟,则返回true
,否则返回 false。请参阅有关不同时钟类型的信息,了解哪些是单调时钟。
自 Qt XML 6.6 起,QElapsedTimer 使用std::chrono::steady_clock
,因此此函数现在总是返回 true。
另请参阅 clockType() 和QElapsedTimer::ClockType 。
[noexcept]
bool QElapsedTimer::isValid() const
如果计时器从未启动或调用invalidate() 后无效,则返回false
。
另请参阅 invalidate()、start() 和restart()。
[noexcept]
qint64 QElapsedTimer::msecsSinceReference() const
返回QElapsedTimer 对象上次启动时间与其参考时钟启动时间之间的毫秒数。
对于所有时钟(QElapsedTimer::SystemTime 时钟除外)来说,这个毫秒数通常是任意的。对于该时钟类型,这个数字是自 1970 年 1 月 1 日 0:00 UTC 起的毫秒数(即以毫秒表示的 Unix 时间)。
在 Linux、Windows 和 Apple 平台上,该值通常是系统启动后的时间,但通常不包括系统在睡眠状态下的时间。
[noexcept]
qint64 QElapsedTimer::msecsTo(const QElapsedTimer &other) const
返回QElapsedTimer 和other 之间的毫秒数。如果other 在此对象之前启动,返回值将为负数。如果在此之后启动,则返回值为正数。
如果此对象或other 已失效,则返回值未定义。
[noexcept]
qint64 QElapsedTimer::nsecsElapsed() const
返回QElapsedTimer 上次启动后的纳秒数。
在QElapsedTimer 无效时调用此函数会导致未定义的行为。
在不提供纳秒分辨率的平台上,返回值将是可用的最佳估计值。
另请参阅 start()、restart()、hasExpired() 和invalidate()。
[noexcept]
qint64 QElapsedTimer::restart()
重新启动定时器,并返回自上次启动后的毫秒数。该函数等同于使用elapsed() 获取经过的时间,然后使用start() 再次启动定时器,但它只需一次操作,避免了两次获取时钟值的需要。
在无效的QElapsedTimer 上调用该函数会导致未定义的行为。
下面的示例说明了如何使用该函数来校准慢速操作(例如迭代计数)的参数,使该操作至少需要 250 毫秒:
QElapsedTimer timer; int count = 1; timer.start(); do { count *= 2; slowOperation2(count); } while (timer.restart() < 250); return count;
另请参阅 start()、invalidate()、elapsed() 和isValid()。
[noexcept]
qint64 QElapsedTimer::secsTo(const QElapsedTimer &other) const
返回QElapsedTimer 和other 之间的秒数。如果other 在此对象之前启动,返回值将为负数。如果在此之后启动,则返回值为正数。
在QElapsedTimer 上或使用无效的 调用此函数将导致未定义的行为。
[noexcept]
void QElapsedTimer::start()
启动该计时器。启动后,可以使用elapsed() 或msecsSinceReference() 检查定时器值。
通常情况下,定时器是在进行冗长的操作前启动的,例如:"......":
QElapsedTimertimer; timer.start(); slowOperation1(); qDebug() << "The slow operation took" << timer.elapsed() << "milliseconds";
此外,启动计时器可使其再次有效。
另请参阅 restart()、invalidate() 和elapsed()。
相关非会员
[noexcept]
bool operator!=(const QElapsedTimer &lhs, const QElapsedTimer &rhs)
如果lhs 和rhs 包含不同的时间,则返回true
,否则返回 false。
[noexcept]
bool operator<(const QElapsedTimer &lhs, const QElapsedTimer &rhs)
如果lhs 在rhs 之前启动,则返回true
,否则返回 false。
如果两个参数中一个无效,另一个无效,则返回值未定义。但是,两个无效计时器是相等的,因此该函数将返回 false。
[noexcept]
bool operator==(const QElapsedTimer &lhs, const QElapsedTimer &rhs)
如果lhs 和rhs 包含相同的时间,则返回true
,否则返回 false。
© 2025 The Qt Company Ltd. Documentation contributions included herein are the copyrights of their respective owners. The documentation provided herein is licensed under the terms of the GNU Free Documentation License version 1.3 as published by the Free Software Foundation. Qt and respective logos are trademarks of The Qt Company Ltd. in Finland and/or other countries worldwide. All other trademarks are property of their respective owners.