Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Starting a second Timer after the first Timer's loop times out

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"
    }

  }

}

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>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();
        }
    }
}
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading