Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. QML and Qt Quick
  4. Create button to counting
Forum Updated to NodeBB v4.3 + New Features

Create button to counting

Scheduled Pinned Locked Moved Solved QML and Qt Quick
3 Posts 2 Posters 691 Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • I Offline
    I Offline
    isan
    wrote on 29 Nov 2019, 12:04 last edited by isan
    #1

    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{

    O 1 Reply Last reply 29 Nov 2019, 12:15
    0
    • I isan
      29 Nov 2019, 12:04

      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{

      O Offline
      O Offline
      ODБOï
      wrote on 29 Nov 2019, 12:15 last edited by ODБOï
      #2

      @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()
              }
          }
      
      I 1 Reply Last reply 3 Dec 2019, 19:58
      2
      • O ODБOï
        29 Nov 2019, 12:15

        @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()
                }
            }
        
        I Offline
        I Offline
        isan
        wrote on 3 Dec 2019, 19:58 last edited by isan 12 Mar 2019, 21:20
        #3

        @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()
                 }
        
        1 Reply Last reply
        0

        3/3

        3 Dec 2019, 19:58

        • Login

        • Login or register to search.
        3 out of 3
        • First post
          3/3
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • Users
        • Groups
        • Search
        • Get Qt Extensions
        • Unsolved