如果列表元素包含超过 1 个值,组合框不显示数据
Combobox doesnt display data if listelement contains more than 1 value
我正在按照 https://doc.qt.io/qt-5/qml-qtquick-controls-combobox.html 中的 Combobox 示例进行操作。
如果Listmodel 中的ListElement 包含超过1 个项目,Combobox 显示空白。有人可以指出我做错了什么还是错误?我在我的代码部分添加了注释,哪些有效,哪些无效。
import QtQuick 2.12
import QtQuick.Controls 2.4
import QtQuick.Layouts 1.3
Page {
id : somepageid
// This works
ComboBox {
id : first
width: 200
model: [ "Banana", "Apple", "Coconut" ]
}
// This also works
ComboBox {
id : second
anchors.top : first.bottom
anchors.topMargin: 10
currentIndex: 2
model: ListModel {
id: cbItems
ListElement { text: "Banana" }
ListElement { text: "Apple" }
ListElement { text: "Coconut" }
}
width: 200
onCurrentIndexChanged: console.debug(cbItems.get(currentIndex).text + ", " + cbItems.get(currentIndex).color)
}
// This doesnt work
// Need help here
ComboBox {
id : third
anchors.top : second.bottom
anchors.topMargin: 10
currentIndex: 2
model: ListModel {
id: cbItems2
ListElement { text: "Banana"; color: "Yellow" }
ListElement { text: "Apple"; color: "Green" }
ListElement { text: "Coconut"; color: "Brown" }
}
width: 200
onCurrentIndexChanged: console.debug(cbItems.get(currentIndex).text + ", " + cbItems.get(currentIndex).color)
}
}
您提供的 link 演示了如何将 ComboBox
用于 Qt Quick Controls 1。根据该 Qt 页面 (https://doc.qt.io/qt-5/qtquickcontrols1-index.html):
Warning: The Qt Quick Controls 1 module is deprecated since Qt 5.12.
Use the latest Qt Quick Controls module instead.
通常情况下,您会这样做 - 您的导入语句是 import QtQuick.Controls 2.4
,因此您使用的是 版本 2 中的 ComboBox
,在此处进行了描述:https://doc.qt.io/qt-5/qml-qtquick-controls2-combobox.html.
它与 ComboBox
v1 略有不同。为了使您的示例工作,您需要指定 textRole
(https://doc.qt.io/qt-5/qml-qtquick-controls2-combobox.html#textRole-prop),即。 textRole: "text"
你的情况。
但是,版本 1 的示例中没有错误 - 要按照所述工作 CombBox
,您只需要导入 QtQuick.Controls
版本 1。然后它将在没有 [=14 的情况下工作=].
我正在按照 https://doc.qt.io/qt-5/qml-qtquick-controls-combobox.html 中的 Combobox 示例进行操作。
如果Listmodel 中的ListElement 包含超过1 个项目,Combobox 显示空白。有人可以指出我做错了什么还是错误?我在我的代码部分添加了注释,哪些有效,哪些无效。
import QtQuick 2.12
import QtQuick.Controls 2.4
import QtQuick.Layouts 1.3
Page {
id : somepageid
// This works
ComboBox {
id : first
width: 200
model: [ "Banana", "Apple", "Coconut" ]
}
// This also works
ComboBox {
id : second
anchors.top : first.bottom
anchors.topMargin: 10
currentIndex: 2
model: ListModel {
id: cbItems
ListElement { text: "Banana" }
ListElement { text: "Apple" }
ListElement { text: "Coconut" }
}
width: 200
onCurrentIndexChanged: console.debug(cbItems.get(currentIndex).text + ", " + cbItems.get(currentIndex).color)
}
// This doesnt work
// Need help here
ComboBox {
id : third
anchors.top : second.bottom
anchors.topMargin: 10
currentIndex: 2
model: ListModel {
id: cbItems2
ListElement { text: "Banana"; color: "Yellow" }
ListElement { text: "Apple"; color: "Green" }
ListElement { text: "Coconut"; color: "Brown" }
}
width: 200
onCurrentIndexChanged: console.debug(cbItems.get(currentIndex).text + ", " + cbItems.get(currentIndex).color)
}
}
您提供的 link 演示了如何将 ComboBox
用于 Qt Quick Controls 1。根据该 Qt 页面 (https://doc.qt.io/qt-5/qtquickcontrols1-index.html):
Warning: The Qt Quick Controls 1 module is deprecated since Qt 5.12. Use the latest Qt Quick Controls module instead.
通常情况下,您会这样做 - 您的导入语句是 import QtQuick.Controls 2.4
,因此您使用的是 版本 2 中的 ComboBox
,在此处进行了描述:https://doc.qt.io/qt-5/qml-qtquick-controls2-combobox.html.
它与 ComboBox
v1 略有不同。为了使您的示例工作,您需要指定 textRole
(https://doc.qt.io/qt-5/qml-qtquick-controls2-combobox.html#textRole-prop),即。 textRole: "text"
你的情况。
但是,版本 1 的示例中没有错误 - 要按照所述工作 CombBox
,您只需要导入 QtQuick.Controls
版本 1。然后它将在没有 [=14 的情况下工作=].