INCREMENT Variable in different buttons



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


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



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



  • @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



  • @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 ...



  • @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.



  • @stcorp what is "index"?



  • @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.



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



  • @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
            }
        }
    }
    

Log in to reply