当我尝试初始化 ComboBox 的模型时,弹出奇怪的错误
test.pro
# Add more folders to ship with the application, here
folder_01.source = qml/androidTest
folder_01.target = qml
DEPLOYMENTFOLDERS = folder_01
#QMAKE_CXXFLAGS += -std=c++0x
CONFIG += c++11
QT += qml quick
# The .cpp file which was generated for your project. Feel free to hack it.
SOURCES += main.cpp
OTHER_FILES += \
qml/androidTest/main.qml
主文件
#include <QtGui/QGuiApplication>
#include <QQuickView>
int main(int argc, char *argv[])
{
QGuiApplication app(argc, argv);
QQuickView view;
view.setSource(QUrl("/Users/Qt/program/experiment_apps_and_libs/test/qml/test/main.qml"));
view.show();
return app.exec();
}
main1.qml
import QtQuick 2.2
import QtQuick.Controls 1.1
Rectangle {
width: 100
height: 62
ListModel{
id: modelA
}
ComboBox{
model: modelA
}
Component.onCompleted: {
modelA.append({"source" : "hhhh"})
}
}
错误信息
file:///C:/Qt/Qt5.2.0/5.2.0/mingw48_32/qml/QtQuick/Controls/ComboBox.qml:496:TypeError:无法读取未定义的属性“构造函数”
我该如何解决这个错误?
编辑 1:
我没有制作内联模型,因为我想将模型的构造和 ComboBox 分开。我的英语不好解释,这是一个简单的例子
文本CB
Column{
id: root
function appendUnitModel(units){
for(var i = 0; i != units.length; ++i){
unitModel.append({"unit": units[i]});
}
}
property alias inputText: input.text
SystemPalette{id: palette}
ListModel{
id: unitModel
}
Row{
spacing: 5
Text{
id: input
color: palette.highlight
height: root.height / 2
width: root.width * 0.6
focus: true
font.family: "Helvetica"
font.pixelSize: 16; font.bold: true
//Behavior on height{ NumberAnimation{duration: 500} }
MouseArea{
anchors.fill: parent
onClicked: {
showKeyBoard()
}
}
}
ComboBox{
id: unitSelector
model: unitModel
editable: true
height: input.height
width: root.width - input.width
}
}
}
main2.qml
TextCB{
id: inputAndClear
height: root.height * 0.2
width: root.width
Component.onCompleted: {
var units = ["meters", "decimeters", "centimeters",
"millimeters", "kilometers", "inches",
"feet", "yards", "miles", "nautical miles",
"cables"]
inputAndClear.appendUnitModel(units)
}
}
将模型和 ComboBox 的构造分开,我可以更轻松地重用它。
编辑 2:对于那些不使用 QtCreator 的人,这里是命令行
- /Users/yyyy/Qt5.2.0/5.2.0/clang_64/bin/qmake -makefile -d test.pro
- 制作
- cd androidTest.app/Contents/MacOS
- lldb 测试
- 跑
此命令在 OSX 下,您可能需要在不同的操作系统下对其进行一些调整(例如:将 lldb 更改为 gdb)