Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

Create button to counting



  • I'm new in QML ,I have 3 buttons that should count to 3,2,1 when clicked on and then each of them has its own functions to call
    And I want, for example, when the first button was selected and the counting started
    If the second button is selected too, the first button will be unchecked and stop counting and the second will operate and start counting but I don't know how to do this!

    import QtQuick 2.9
    import QtQuick.Layouts 1.3
    import QtQuick.Controls 2.3
    
    Button {
        id: button
        checkable: true
       // font.pixelSize: fontSizeExtraSmall
        leftPadding: 4
        rightPadding: 4
        topPadding: 12
        bottomPadding: 12
        implicitWidth: 90
        implicitHeight: 90
    
        icon.name: "placeholder"
        icon.width: 44
        icon.height: 44
        display: Button.TextUnderIcon
    
    
        onClicked: Text{
            id: name
                property int cnt: 3
                text:cnt
            function decrementCnt(){
                if (cnt>0)
                cnt= cnt-1
            }
        }
            Timer {
                running: true
                onTriggered: button.decrementCnt()
            }
    
    }
    

    I have a error in onClicked: Text{



  • @isan hi

    @isan said in Create button to counting:

    And I want, for example, when the first button was selected and the counting started

    first do only one button that counts from 3 to 1, then you will write the logic to stop one when you start another

    you did syntax error at

     onClicked: Text{
            id: name
    

    after the onClicked: you have to write javascript expression/ call js function,

     Button{
            id: btn
             property int cnt: 3
             text:  cnt
    
            function decrementCnt(){
                if (cnt>0){
                   cnt--
                }else{
                    decrementCntTimer.stop()
                }
            }
    
            Timer {
                id: decrementCntTimer
                repeat: true
                interval: 1000 // 1 second
                onTriggered:{
                    console.log("decrementing.")
                    btn.decrementCnt()
                }
            }
    
            onClicked: {
                decrementCntTimer.start()
            }
        }
    


  • @LeLev Thank You, I did this because I want to show the count on the button text
    Now I do this to show it

    onTriggered:{
                console.log("decrementing.")
                btn.text= cnt
                btn.decrementCnt()
             }
    

Log in to reply