本页内容

QElapsedTimer Class

QElapsedTimer 类提供了一种快速计算经过时间的方法。更多内容...

头文件: #include <QElapsedTimer>
CMake: find_package(Qt6 REQUIRED COMPONENTS Core)
target_link_libraries(mytarget PRIVATE Qt6::Core)
qmake: QT += core

注意:该类中的所有函数均为可重入的。

QElapsedTimer 比较

类别可比较类型
strongQElapsedTimer

公共类型

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 对象转换为人类可读的时间格式。

该类的典型用例是确定某项耗时操作所花费的时间。此类用例最简单的示例是用于调试,如下例所示:

    QElapsedTimer timer;
    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 在特定机器上,QElapsedTimer 始终使用同一类时钟,因此该值在程序生命周期内不会改变。提供此枚举是为了使 能够与其他非 Qt 实现配合使用,从而保证使用的是同一参考时钟。

常量常量名描述
QElapsedTimer::SystemTime0易于人类阅读的系统时间。该时钟并非单调的。
QElapsedTimer::MonotonicClock1系统的单调时钟,通常存在于 Unix 系统中。该时钟是单调的。
QElapsedTimer::TickCounter2已不再使用。
QElapsedTimer::MachAbsoluteTime3Mach内核的绝对时间(macOS和iOS)。该时钟具有单调性。
QElapsedTimer::PerformanceCounter4Windows 提供的性能计数器。该时钟具有单调性。
SystemTime

系统时间时钟纯粹表示实时,以自 1970 年 1 月 1 日 0:00 UTC 起经过的毫秒数表示。 它等同于 C 和 POSIXtime 函数返回的值,外加千毫秒。该时钟类型目前仅用于不支持单调时钟的 Unix 系统(见下文)。

这是QElapsedTimer 可能使用的唯一非单调时钟。

MonotonicClock

这是系统的单调时钟,以自过去某个任意时间点起计算的毫秒数表示。此时钟类型用于支持 POSIX 单调时钟(_POSIX_MONOTONIC_CLOCK )的 Unix 系统。

MachAbsoluteTime

该时钟类型基于 Mach 内核(如 macOS 中的 Mach 内核)提供的绝对时间。由于 macOS 和 iOS 也是 Unix 系统,且可能支持与 Mach 绝对时间值不同的 POSIX 单调时钟,因此该时钟类型与 MonotonicClock 分开呈现。

该时钟具有单调性。

PerformanceCounter

该时钟使用 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 default] QElapsedTimer::QElapsedTimer()

构建一个无效的 QElapsedTimer。计时器在启动后即变为有效。

另请参阅 isValid() 和start()。

[static noexcept] QElapsedTimer::ClockType QElapsedTimer::clockType()

返回此QElapsedTimer 实现所使用的时钟类型。

自 Qt 6.6 起,QElapsedTimer 采用std::chrono::steady_clock ,因此时钟类型始终为MonotonicClock 。

另请参阅 isMonotonic()。

[noexcept, since 6.6] QElapsedTimer::Duration QElapsedTimer::durationElapsed() const

返回一个std::chrono::nanoseconds ,其中包含自该QElapsedTimer 上次启动以来经过的时间。

若对无效的QElapsedTimer 调用此函数,将导致未定义行为。

在不支持纳秒级精度的平台上,返回值将为可获得的最佳估计值。

该函数在 Qt 6.6 中引入。

另请参阅 start()、restart()、hasExpired() 和invalidate()。

[noexcept, since 6.6] QElapsedTimer::Duration QElapsedTimer::durationTo(const QElapsedTimer &other) const

返回此QElapsedTimer 与other 之间的时间差,返回类型为std::chrono::nanoseconds 。如果other 的启动时间早于此对象,则返回值为负数;如果启动时间晚于此对象,则返回值为正数。

如果该对象或other 已被失效,则返回值未定义。

该函数在 Qt 6.6 中引入。

另请参阅 secsTo() 和elapsed()。

[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 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 平台上,该值通常表示自系统启动以来的时间,但通常不包括系统处于睡眠状态所花费的时间。

另请参阅 clockType() 和elapsed()。

[noexcept] qint64 QElapsedTimer::msecsTo(const QElapsedTimer &other) const

返回此QElapsedTimer 与other 之间的时间间隔(以毫秒为单位)。如果other 的启动时间早于此对象,则返回值为负数;如果晚于此对象,则返回值为正数。

如果此对象或other 已被失效,则返回值未定义。

另请参阅 secsTo() 和elapsed()。

[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 ` 调用此函数,将导致未定义的行为。

另请参阅 msecsTo() 和elapsed()。

[noexcept] void QElapsedTimer::start()

启动此计时器。启动后,可通过elapsed() 或msecsSinceReference() 获取计时器值。

通常,会在执行耗时较长的操作之前启动计时器,例如:

    QElapsedTimer timer;
    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。

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