QML Button exclusiveGroup property?

Dav*_*ita 0 qt qml qtquick2

I'm unable to assign ExclusiveGroup to my checkable buttons.

ExclusiveGroup {
    id: group

    onCurrentChanged: {
        if(current != null) {
            console.info("button checked...no!")
            current = null
            //current.checked = false    <--- also this
        }
    }
}


            Column {
                width: parent.width
                spacing: 0

                Button {
                    id: btnScan
                    flat: true
                    text: qsTr("But1")
                    width: parent.width
                    checkable: true
                    exclusiveGroup: group
                }
                Button {
                    id: btnWhiteList
                    flat: true
                    text: qsTr("But2")
                    width: parent.width
                    checkable: true
                    exclusiveGroup: group
                }
}
Run Code Online (Sandbox Code Playgroud)

The documentation states clearly that Button does have exclusiveGroup property http://doc.qt.io/qt-5/qml-qtquick-controls-button.html#exclusiveGroup-prop. However, when I run the example, I get this error:

qrc:/main.qml:48 Cannot assign to non-existent property "exclusiveGroup"
Run Code Online (Sandbox Code Playgroud)

Hovering mouse over "exclusiveGroup" makes a tooltip show up saying: "Invalid property name exclusiveGroup".

I have Qt 5.9.1 installed. Here's my import statements:

import QtQuick 2.7
import QtQuick.Controls 1.4
import QtQuick.Controls 2.0
import QtQuick.Layouts 1.3
import QtQuick.Controls.Material 2.2
Run Code Online (Sandbox Code Playgroud)

What am I doing wrong?

Thanks

der*_*erM 5

你的问题的原因是这样的:

import QtQuick.Controls 1.4
import QtQuick.Controls 2.0
Run Code Online (Sandbox Code Playgroud)

你有Button两个,但他们有一个不同的API。
所以首先你导入ButtonfromQtQuick.Controls 1.4。然后你导入ButtonfromQtQuick.Controls 2.0。由于 QML 不知道您想使用哪一个,它将使用那个,您导入了最后一个。如果你想更具体,Button你想使用,你可以使用命名导入

import QtQuick.Controls 1.4 as OldCtrl
import QtQuick.Controls 2.0 as NewCtrl
Run Code Online (Sandbox Code Playgroud)

然后您可以根据需要使用Button两个版本中的 s :

OldCtrl.Button { // from the QtQuick.Controls 1.4 }
NewCtrl.Button { // from the QtQuick.Controls 2.0 }
Run Code Online (Sandbox Code Playgroud)

您引用的文档是针对 的QtQuick.Controls 1.x,从那里导入的ExclusiveGroup. 所以你试图混合来自两个库的东西,这不会一起工作。

有关ButtonGroup类似的解决方案,请参阅QtQuick.Controls 2.x

有关两组控件的差异和用例的更多信息,请阅读: