我在 ExclusiveGroup
中有三个按钮:
ExclusiveGroup {id: group}
Button{
checkable: true
exclusiveGroup: group
}
Button{
checkable: true
exclusiveGroup: group
}
Button{
checkable: true
exclusiveGroup: group
}
在我点击其中任何一个之前,它们显然都是未选中的,但是一旦其中一个被选中,我该如何取消选中它们呢?我是否真的需要添加另一个按钮,一旦选中,就会产生在其他按钮未选中时适用的行为?
最佳答案
您可以利用 current
ExclusiveGroup
的属性:
The currently selected object. Defaults to the first checked object bound to the ExclusiveGroup. If there is none, then it defaults to null.
因此,方法是在需要时通过将 current
属性设置为 null
来取消选中当前按钮。
在下面的例子中,我删除了检查状态,这显然更像是一个练习,而不是一个真实的用例。但无论如何,它提供了一种方法的把握:
import QtQuick 2.4
import QtQuick.Window 2.0
import QtQuick.Controls 1.2
ApplicationWindow {
id: window
visible: true
width: 100
height: 200
ColumnLayout {
anchors.centerIn: parent
Button{
id: but1
checkable: true
exclusiveGroup: group
}
Button{
id: but2
checkable: true
exclusiveGroup: group
}
Button{
id: but3
checkable: true
exclusiveGroup: group
}
}
ExclusiveGroup {
id: group
onCurrentChanged: {
if(current != null) {
console.info("button checked...no!")
current = null
//current.checked = false <--- also this
}
}
}
}
关于qt - QML取消选中ExclusiveGroup中的选中按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29139634/