[英]Uncheck button when buttons are exclusive in Qt Quick Controls 2
我需要列出應該排他的可檢查AbstractButton
的列表,但是默認情況下,我無法取消選中未選中任何按鈕的選中按鈕。
現在,我必須做這樣的事情來模仿這樣的邏輯:
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
}
}
}
}
...
}
這很丑陋,找到更好的解決方案將是很棒的。
您可以通過強制釋放按鈕來取消選中它,以使其具有不可檢查的獨占按鈕:
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.
}
}
}
這個怎么樣:
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")
}
}
請注意,即使從其他位置設置了choice
,或者您在GUI中的多個按鈕組中都有該按鈕組(只要它們都共享相同的choice
,GUI也會更新屬性。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.