组合框初始化错误:无法读取未定义的属性'constructor'

initialize error of ComboBox : Cannot read property 'constructor' of undefined

本文关键字:属性 constructor 未定义 读取 初始化 错误 组合      更新时间:2023-10-16

当我尝试初始化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的人,这里是命令行

  1. /Users/yyyy/Qt5.2.0/5.2.0/clang_64/bin/qmake-makefile-d test.pro
  2. 制造
  3. cd androidTest.app/Contents/MacOS
  4. lldb测试
  5. 运行

此命令在OSX下,您可能需要在不同的操作系统下对其进行一点调整(例如:将lldb更改为gdb)

问题是您试图设置ListModelListElement的"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/m‌​ain.qml"));

我个人在本地简单地将其更改为:

view.setSource(QUrl("m‌​ain.qml"));

此外,对于这个实验设置,您似乎有不必要的qmake选项,如下所示:

CONFIG += c++11

QT += qml quick

对于后者,不需要显式指定qml