pra7
pra7

Reputation: 884

Setting Checked property of a Button in QML

I am having a group of buttons in a Column and i have set autoExclusive : true. Now only one button can be checked as expected. But, how to disable the checked state if i click on the button which is already checked? Following is the code:

Column {
    id: column

    Button {
        checked: true
        text: qsTr("button 1")
        autoExclusive : true
        checkable : true
        background: Rectangle {
            color:checked ? "red" : "white"
        }
    }

    Button {
        checked: true
        text: qsTr("button 2")
        autoExclusive : true
        checkable : true
        background: Rectangle {
            color:checked ? "red" : "white"
        }
    }

    Button {
        checked: true
        text: qsTr("button 3")
        autoExclusive : true
        checkable : true
        background: Rectangle {
            color:checked ? "red" : "white"
        }
    }
}

Upvotes: 2

Views: 6698

Answers (2)

user14382841
user14382841

Reputation: 1

color: button2.checked ? "red" : "white"

Upvotes: 0

pra7
pra7

Reputation: 884

There is a way to do this by using ButtonGroup :

Column {
    id: column

    Button {
        checked: true
        text: qsTr("button 1")
        ButtonGroup.group: btnGrp //assign buttongroup
        checkable : true
        background: Rectangle {
            color:checked ? "red" : "white"
        }
    }

    Button {
        checked: true
        text: qsTr("button 2")
        ButtonGroup.group: btnGrp //assign buttongroup
        checkable : true
        background: Rectangle {
            color:checked ? "red" : "white"
        }
    }

    Button {
        checked: true
        text: qsTr("button 3")
        ButtonGroup.group: btnGrp //assign buttongroup
        checkable : true
        background: Rectangle {
            color:checked ? "red" : "white"
        }
    }
}

ButtonGroup {
    id:btnGroup
}

Now loop through btnGrp.buttons and can check the button state to true or false and also can get checked button by accessing btnGrp.checkedButton.

Upvotes: 3

Related Questions