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

标签 qt qml qt5

我在 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/

相关文章:

c++ - 如何在类中使用数组?

qt - QML ListView 不会在鼠标单击时响应

c++ - 从 C++ 程序控制闪存系统

qt - 如何制作跟踪/控制任何 bool 属性的 QML 切换按钮

c++ - 在 Qt QML 中为许多文本元素指定字体

c++ - windeployqt 不会为调试应用程序部署 qwindowsd.dll

Qt 样式表,来自文件路径的背景图像

qt4 - QML 自定义属性

c++ - 如何在 Qt5 中使用带有模型/ View /委托(delegate)的自定义小部件?

c++ - 从表单的小部件收集值