table of Contents
The timer can be used to trigger an action, triggering repeated once or within a given time interval, in embedded microcontroller is also very common. Simply set a cycle time value, the value will overflow triggering event, such as setting up the alarm values, time, alarm clock, uh, get up and work friends - after closing, if you set the repeat, then had a day Oh, get up early ~
1. Examples
Direct Push Example:
import QtQuick 2.0
import QtQuick.Window 2.12
import QtQuick.Controls 1.4
Window {
id: root;
visible: true;
width: 400;
height: 200;
Text {
id: cnt;
anchors.centerIn: parent;
color: "red";
font.pixelSize: 24; //像素大小
property int seconds : 4;
}
Timer {
id: tim;
interval: 1000;
// running: true;
repeat: true
triggeredOnStart: true;
onTriggered: {
cnt.text = cnt.seconds;
cnt.seconds --;
if(cnt.seconds <0)
{
tim.stop();
cnt.text = "Time out!!!"
}
}
}
//开始
Button {
id: startBtn;
anchors.bottom: cnt.top;
anchors.bottomMargin: 30;
anchors.right: stopBtn.left;
anchors.rightMargin: 10;
text: "Start";
onClicked: {
tim.start();
cnt.seconds = 4;
}
}
//停止
Button {
id: stopBtn;
anchors.bottom: cnt.top;
anchors.bottomMargin: 30;
anchors.horizontalCenter: cnt.horizontalCenter;
text: "Stop";
onClicked: {
tim.stop();
}
}
//重新开始
Button {
id: restartBtn;
anchors.bottom: cnt.top;
anchors.bottomMargin: 30;
anchors.left: stopBtn.right;
anchors.leftMargin: 10;
text: "Restart";
onClicked: {
tim.restart();
}
}
}
2. Run results
Start and stop interface Interface:
restart the interface to the interface and time:
3. Timer details
Import declarations
import QtQml 2.12
Attributes
● interval: int
set the interval between the trigger milliseconds.
The default interval: 1000 ms.
● repeat: bool
as true, it will trigger the timer repeats; otherwise trigger only once and then stop (ie running is set to false).
repeat, the default value is false.
● running: bool
as is true, then start the timer; otherwise stop the timer. For not repeating timer, after trigger the timer running is set to false.
running, default is false.
● triggeredOnStart: bool
when the timer is started, would have to wait after the timer starts at set intervals before triggering. If triggeredOnStart true, when you start the timer is triggered immediately, then at a specified time interval trigger.
Note that if repeat is set to false, the timer is triggered twice; once to start, after one cycle.
triggeredOnStart, default is false.
signal
● triggered ()
timer is triggered after a timeout. Corresponding to the handler "onTriggered"
method
● restart ()
restarts the timer. If the timer is not running, it will be started, otherwise it will reset to the initial state and starts.
● start ()
to start the timer. If the timer is already running, then invoking this method.
● stop ()
to stop the timer. If the timer is not running, then invoking this method.
[Link] Reference: mouse event handlers of Qt Quick, keyboard, timer
QT QUICK QML chicken dish Tutorial:
● QT Quick QML introductory notes (a) structural analysis applications and QML basis
● QT Quick QML entry Notes (b) signals and slots
● QT Quick QML introductory notes (three) common elements
● QT Quick QML introductory notes (d) anchor (anchors) layout
● QT Quick QML introductory notes (five) handling mouse and keyboard events