QML 取消选中 ExclusiveGroup 中的选中按钮

Eve*_*sle 5 qt qml qt5

我有三个按钮,位于ExclusiveGroup

ExclusiveGroup {id: group}
Button{
    checkable: true
    exclusiveGroup: group
}
Button{
    checkable: true
    exclusiveGroup: group
}
Button{
    checkable: true
    exclusiveGroup: group
}
Run Code Online (Sandbox Code Playgroud)

在我单击其中任何一个之前,它们显然都未被选中,但是一旦选中其中一个,我如何才能取消选中它们呢?我是否真的需要添加另一个按钮,一旦选中该按钮,就会产生在未选中其他按钮时应用的行为?

BaC*_*Zzo 3

您可以利用current以下属性ExclusiveGroup

当前选定的对象。默认为绑定到 ExclusiveGroup 的第一个检查的对象。如果没有默认为 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
            }
        }
    }
}
Run Code Online (Sandbox Code Playgroud)