Reputation: 990
I need to make list of checkable AbstractButton
's that should be exclusive ,but by default I cannot uncheck checked button with no one button checked.
Now I have to make something like this to imitate such logic:
Item {
AbstractButton {
id: oneButton
checkable: true
onCheckedChanged: {
if(checked) {
if(twoButton.checked || threeButton.checked || ...) {
twoButton.checked = threeButton.checked = ... = false
}
}
}
}
AbstractButton {
id: twoButton
checkable: true
onCheckedChanged: {
if(checked) {
if(oneButton.checked || threeButton.checked || ...) {
oneButton.checked = threeButton.checked = ... = false
}
}
}
}
...
}
This is ugly and it would be great to find better solution.
Upvotes: 2
Views: 2073
Reputation: 7170
You can have an uncheckable exclusive button by forcing it to uncheck on release :
Button {
checkable: true
autoExclusive: true
property bool wasChecked
onPressed: wasChecked = checked
onReleased: {
if (wasChecked) {
checked = false;
toggled(); // emit the toggled signal manually, since we changed the checked value programmatically but it still originated as an user interaction.
}
}
}
Upvotes: 5
Reputation: 49329
How about this:
Column {
id: col
spacing: 2
property int choice: -1
Repeater {
model: 5
delegate: Button {
checkable: true
checked: col.choice === index
onClicked: col.choice = (col.choice === index ? -1 : index)
text: "Button " + index
}
}
Text {
text: "Choice is " + (col.choice > -1 ? col.choice : "undefined")
}
}
Note that the GUI will update property even if choice
is set from somewhere else, or if you have that button group in more than one place in the GUI, as long as they all share the same choice
.
Upvotes: 2