QML Button exclusiveGroup 属性?
QML Button exclusiveGroup property?
我无法将 ExclusiveGroup 分配给我的可检查按钮。
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
}
}
文档明确指出 Button 确实有 exclusiveGroup 属性 http://doc.qt.io/qt-5/qml-qtquick-controls-button.html#exclusiveGroup-prop。但是,当我 运行 这个例子时,我得到这个错误:
qrc:/main.qml:48 Cannot assign to non-existent property "exclusiveGroup"
将鼠标悬停在 "exclusiveGroup" 上会显示工具提示:"Invalid property name exclusiveGroup"。
我安装了 Qt 5.9.1。这是我的导入语句:
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
我做错了什么?
谢谢
您遇到问题的原因是:
import QtQuick.Controls 1.4
import QtQuick.Controls 2.0
你在两者中都有 Button
,但他们有不同的 API。
所以首先你导入 Button
from QtQuick.Controls 1.4
. Then you import the Button
from QtQuick.Controls 2.0
。由于 QML 不知道,您要使用哪一个,它会采用您导入的最后一个。如果你想更具体一点,你想在哪个 Button
上使用,你可以使用 named imports
import QtQuick.Controls 1.4 as OldCtrl
import QtQuick.Controls 2.0 as NewCtrl
然后您可以根据需要使用两个版本的 Button
s:
OldCtrl.Button { // from the QtQuick.Controls 1.4 }
NewCtrl.Button { // from the QtQuick.Controls 2.0 }
您引用的文档是针对 QtQuick.Controls 1.x
的,并且从那里导入了 ExclusiveGroup
。所以你试图混合来自两个库的东西,它们不能一起工作。
有关 QtQuick.Controls 2.x
的类似解决方案,请参阅 ButtonGroup
有关两组控件的差异和用例的更多信息,请阅读:
我无法将 ExclusiveGroup 分配给我的可检查按钮。
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
}
}
文档明确指出 Button 确实有 exclusiveGroup 属性 http://doc.qt.io/qt-5/qml-qtquick-controls-button.html#exclusiveGroup-prop。但是,当我 运行 这个例子时,我得到这个错误:
qrc:/main.qml:48 Cannot assign to non-existent property "exclusiveGroup"
将鼠标悬停在 "exclusiveGroup" 上会显示工具提示:"Invalid property name exclusiveGroup"。
我安装了 Qt 5.9.1。这是我的导入语句:
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
我做错了什么?
谢谢
您遇到问题的原因是:
import QtQuick.Controls 1.4
import QtQuick.Controls 2.0
你在两者中都有 Button
,但他们有不同的 API。
所以首先你导入 Button
from QtQuick.Controls 1.4
. Then you import the Button
from QtQuick.Controls 2.0
。由于 QML 不知道,您要使用哪一个,它会采用您导入的最后一个。如果你想更具体一点,你想在哪个 Button
上使用,你可以使用 named imports
import QtQuick.Controls 1.4 as OldCtrl
import QtQuick.Controls 2.0 as NewCtrl
然后您可以根据需要使用两个版本的 Button
s:
OldCtrl.Button { // from the QtQuick.Controls 1.4 }
NewCtrl.Button { // from the QtQuick.Controls 2.0 }
您引用的文档是针对 QtQuick.Controls 1.x
的,并且从那里导入了 ExclusiveGroup
。所以你试图混合来自两个库的东西,它们不能一起工作。
有关 QtQuick.Controls 2.x
ButtonGroup
有关两组控件的差异和用例的更多信息,请阅读: