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. INCREMENT Variable in different buttons
QtWS25 Last Chance

INCREMENT Variable in different buttons

Scheduled Pinned Locked Moved Unsolved QML and Qt Quick
10 Posts 3 Posters 2.6k 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.
  • T Offline
    T Offline
    Titi01
    wrote on last edited by
    #1

    Hello,
    I have a button.qml file that is used to create a Button.
    In the main.qml I have the code below.
    I would like the variable "a" to be 1 in the first Button and 2 in the second Button
    How should I do it ?
    Thanks

    Button.qml

    Rectangle{
        width:60
        height:75
        border.color: "black"
        border.width: 2
        radius:5
        smooth: true
    }
    

    main.qml

    Rectangle{
      height: 800
      width : 1000
    
      property int a = 0
    
      Button{
        id:button1
        color="blue"      //Here I would like that "a" is worth 1
    }
      Button{
        id:button2
        color="red"        // And here I would like that "a" is worth 2
    }
    
    1 Reply Last reply
    0
    • dheerendraD Offline
      dheerendraD Offline
      dheerendra
      Qt Champions 2022
      wrote on last edited by
      #2

      a is property of main.qml object. You can not do that. If u want that, make a as property of button.qml

      Dheerendra
      @Community Service
      Certified Qt Specialist
      http://www.pthinks.com

      1 Reply Last reply
      4
      • T Offline
        T Offline
        Titi01
        wrote on last edited by
        #3

        @dheerendra If i make a as property of button.qml, can i use a in main.qml?

        S 1 Reply Last reply
        0
        • T Titi01

          @dheerendra If i make a as property of button.qml, can i use a in main.qml?

          S Offline
          S Offline
          stcorp
          wrote on last edited by
          #4

          @Titi01 Yes, but then you would have to give the id of the button you want. So you would write "button1.a" to access button1's "a", or "button2.a" to access "a" of button2.

          But I think you may need to provide a little bit more information on what exactly you want to use this variable for

          1 Reply Last reply
          0
          • T Offline
            T Offline
            Titi01
            wrote on last edited by
            #5

            @stcorp
            In fact I need that when you click on button 2, for example, the variable "a" is 2 ... When you click on the button 52 "a" is worth 52 ...

            S 1 Reply Last reply
            0
            • T Titi01

              @stcorp
              In fact I need that when you click on button 2, for example, the variable "a" is 2 ... When you click on the button 52 "a" is worth 52 ...

              S Offline
              S Offline
              stcorp
              wrote on last edited by
              #6

              @Titi01 What about something like this:

              import QtQuick 2.7
              import QtQuick.Controls 2.0
              
              ApplicationWindow {
                  visible: true
                  width: 500
                  height: 500
                  property var currentlySelectedButton
              
                  GridView {
                      model: 6
                      anchors.top: parent.top
                      anchors.left: parent.left
                      anchors.right: parent.right
                      anchors.bottom: text.top
                      delegate: Button {
                          text: index
                          onClicked: currentlySelectedButton = index
                          checkable: true
                          autoExclusive: true  // So that only 1 button can be selected
                      }
                  }
              
                  Label {
                      id: text
                      anchors.bottom: parent.bottom
                      text: "Currently Selected Button: " + currentlySelectedButton
                  }
              }
              
              

              I used a GridView to help create several buttons, but you could also create all the buttons yourself if you want. Then whenever one of the buttons is clicked, it uses its index to update the "currentlySelectedButton" property in the ApplicationWindow. The label at the bottom displays which button is currently selected. I also made the buttons checkable, so that they will be highlighted when it is selected, and activated the autoExclusive property so that only one button can be checked at a time.

              1 Reply Last reply
              0
              • T Offline
                T Offline
                Titi01
                wrote on last edited by
                #7

                @stcorp what is "index"?

                S 1 Reply Last reply
                0
                • T Titi01

                  @stcorp what is "index"?

                  S Offline
                  S Offline
                  stcorp
                  wrote on last edited by
                  #8

                  @Titi01 "index" is a variable that the GridView (or ListView) automatically gives to each delegate. It is an incrementally increasing integer. So the first item will have index=1, the second item will have index=2, etc.

                  1 Reply Last reply
                  0
                  • T Offline
                    T Offline
                    Titi01
                    wrote on last edited by
                    #9

                    But the problem is that i don't use Gridview... i create all the buttons myself @stcorp

                    S 1 Reply Last reply
                    0
                    • T Titi01

                      But the problem is that i don't use Gridview... i create all the buttons myself @stcorp

                      S Offline
                      S Offline
                      stcorp
                      wrote on last edited by
                      #10

                      @Titi01 Then you can do it something like this:

                      import QtQuick 2.7
                      import QtQuick.Controls 2.0
                      
                      ApplicationWindow {
                          visible: true
                          width: 500
                          height: 500
                          property var currentlySelectedButton
                      
                          Column {
                              anchors.fill: parent
                              Button {
                                  text: "1"
                                  onClicked: currentlySelectedButton = text
                                  checkable: true
                                  autoExclusive: true  // So that only 1 button can be selected
                              }
                              Button {
                                  text: "2"
                                  onClicked: currentlySelectedButton = text
                                  checkable: true
                                  autoExclusive: true  // So that only 1 button can be selected
                              }
                              Button {
                                  text: "3"
                                  onClicked: currentlySelectedButton = text
                                  checkable: true
                                  autoExclusive: true  // So that only 1 button can be selected
                              }
                      
                              Label {
                                  id: label
                                  text: "Currently Selected Button: " + currentlySelectedButton
                              }
                          }
                      }
                      
                      1 Reply Last reply
                      0

                      • Login

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