组合框初始化错误:无法读取未定义的属性'constructor'
initialize error of ComboBox : Cannot read property 'constructor' of undefined
当我尝试初始化ComboBox的模型时,会弹出奇怪的错误
测试.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
main.cpp
#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的结构分开。这很难用我糟糕的英语来解释,这里有一个简单的例子
TextCB
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)
问题是您试图设置ListModel
的ListElement
的"source"属性,而不是它所期望的"text"属性。分别,如果您更改以下行:
modelA.append({"source" : "hhhh"})
至:
modelA.append({"text" : "hhhh"})
它会起作用的。或者,您也可以在组合框中添加以下行,以使您的自定义角色生效:
ComboBox {
model: modelA
textRole: "source"
}
有关的详细说明,请参阅组合框代码
// No text role set, check whether model has a suitable role
// If 'text' is found, or there's only one role, pick that.
您的代码中还存在其他小问题,比如对Windows的qml路径进行硬编码,如下所示。您可以简单地更改为"main.qml",也可以使用资源系统。
view.setSource(QUrl("/Users/Qt/program/experiment_apps_and_libs/test/qml/test/main.qml"));
我个人在本地简单地将其更改为:
view.setSource(QUrl("main.qml"));
此外,对于这个实验设置,您似乎有不必要的qmake选项,如下所示:
CONFIG += c++11
和
QT += qml quick
对于后者,不需要显式指定qml
。
相关文章:
- 如何导出包含具有"std::unique_ptr"值的"std::map"属性的
- C++概念:如何使用'concept'检查模板化结构的属性?
- 子目录是否继承属性,例如add_definitions,include_directories和父Cmakelist.t
- 通过指向指针数组的指针访问子类的属性
- MSVC是否支持C++11样式的属性而不是__declspec
- QML:修改在不同QML文件(而非main.QML)中定义的子对象的属性
- 类的C++属性似乎已重新初始化
- Qt Quick-如何仅从c++代码与qml属性交互
- 用概念检查属性的类型
- C++如何将同一类的实例作为属性
- C++如何通过"constructor initialization"初始化行变量?
- 按多个属性排序
- 主函数参数的属性
- "perf_event_attr"结构的"read_format"属性的选项到底是什么?
- C++删除未使用的类属性会导致 std::logic_error
- 无法使用 SWIG 在 Python 中实例化C++类(获取属性错误)
- 使用内存地址访问结构的属性值
- C++调用具有 *this 属性的单个帮助程序函数
- C++ 在堆栈中包含多态属性的类对象存储
- 组合框初始化错误:无法读取未定义的属性'constructor'