Timer QML Type

在指定时间间隔触发一个处理程序。更多

Import Statement: import QtQml

属性

信号

方法

详细说明

定时器可用于在给定时间间隔内一次或多次触发操作。

下面的计时器显示当前日期和时间,并每隔 500 毫秒更新一次文本。它使用 JavaScriptDate 对象访问当前时间。

import QtQuick 2.0

Item {
    Timer {
        interval: 500; running: true; repeat: true
        onTriggered: time.text = Date().toString()
    }

    Text { id: time }
}

定时器类型与动画定时器同步。由于动画定时器通常设置为 60fps,因此定时器的分辨率最多为 16ms。

如果定时器正在运行,而其属性之一发生了更改,则经过的时间将被重置。例如,如果间隔为 1000 毫秒的定时器在启动 500 毫秒后更改了其重复属性,则经过时间将重置为 0,定时器将在 1000 毫秒后被触发。

另请参阅 Qt Quick 演示 - 时钟

属性文档

interval : int

设置interval 触发间隔,单位为毫秒。

默认间隔为 1000 毫秒。


repeat : bool

如果repeat 为 true,定时器将在指定的时间间隔内重复触发;否则,定时器将在指定的时间间隔内触发一次,然后停止(即运行将设为 false)。

repeat 默认为 false。

另请参阅 running


running : bool

如果设置为 true,则启动定时器;否则停止定时器。对于非重复定时器,running 会在定时器触发后设置为 false。

running 默认为 false。

另请参阅 repeat


triggeredOnStart : bool

定时器启动后,第一次触发通常是在指定的时间间隔过后。有时希望在定时器启动时立即触发,例如,建立初始状态。

如果triggeredOnStart 为真,定时器启动后会立即触发,随后在指定的时间间隔内触发。需要注意的是,如果repeat设置为 false,定时器会被触发两次;一次是在启动时,另一次是在间隔时。

triggeredOnStart 默认为假。

另请参阅 running


信号文档

triggered()

定时器超时时会发出该信号。

注: 相应的处理程序是onTriggered


方法文档

restart()

重启计时器

如果定时器未运行,则会启动,否则会停止、重置为初始状态并启动。调用restart() 后,running 属性将为 true。


start()

启动定时器

如果定时器已在运行,调用此方法不会产生任何影响。调用start() 后,running 属性将为 true。


stop()

停止计时器

如果定时器没有运行,调用此方法没有任何作用。调用stop() 后,running 属性将变为 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.