iam new to qml and iam trying to make 2 timers that change the same text label in a row,
i made the first one that refresh the text every 1 sec with a countdown of 10 sec and i want the second timer to start right after the first timer times out at zero with a new 5 sec from zero in the second timer ,
i used a ternary operator since i couldn’t insert if statements without getting errors, it stops when it reaches zero of the first timer, i think that maybe the problem is that the running member just keeps on looping after it already hit zero,
any help!
import QtQuick 2.15
import QtQuick.Controls 2.15
import QtQuick.Layouts 1.1
Item {
id: root
width: 610
height: 548
property int counter1: 10
property int counter2: 5
ColumnLayout{
anchors.centerIn:parent
spacing: parent
Timer {
id:s1
interval: 1000
running: true
repeat: true
onTriggered: timeLabel.text = (0<counter1 ) ? counter1--:0
}
Timer {
id:s2
interval: 1000
running: true
repeat: true
onTriggered:timeLabel.text =(0<counter2 ) ? counter2--:0
}
Label{
id: timeLabel
color: "#FFFFFF"
font.pixelSize: 100
font.family: "Cherry"
}
}
}
>Solution :
The way you’ve defined your Timers, they will run in parallel at the same time, not sequentially. You need to set the second timer to not run until the first timer tells it to start.
Timer {
id:s1
interval: 1000
running: true
repeat: true
onTriggered: {
timeLabel.text = --counter1;
if (counter1 === 0) {
stop(); // Stop s1
s2.start(); // Start s2
}
}
}
Timer {
id:s2
interval: 1000
running: false // Don't start this yet
repeat: true
onTriggered: {
timeLabel.text = --counter2;
if (counter2 === 0) {
stop();
}
}
}