When I try to initialize the model of ComboBox, weird error pop out
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
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"})
}
}
error message
file:///C:/Qt/Qt5.2.0/5.2.0/mingw48_32/qml/QtQuick/Controls/ComboBox.qml:496: TypeError: Cannot read property 'constructor' of undefined
How could I fix this error?
Edit 1 :
I do not make an inline model because I want to separate the construction of the model and the ComboBox.It is hard to explain by my poor english, here is a simple example
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)
}
}
Separate the construction of the model and ComboBox, I could reuse it more easier.
Edit 2 : For those who do not use QtCreator, here is the command line
This commands are under OSX, you may need to tune it a little bit under different OS(ex : change lldb to gdb)
The problem is that you are trying to set the "source" property for the ListElement
of the ListModel
rather than 'text' what it expects. Respectively, if you change the following line:
modelA.append({"source" : "hhhh"})
to:
modelA.append({"text" : "hhhh"})
it will work. Alternatively, you could also add the following line to your ComboBox to get your custom role taking effect:
ComboBox {
model: modelA
textRole: "source"
}
See the ComboBox code for detailed explanation about it:
// No text role set, check whether model has a suitable role
// If 'text' is found, or there's only one role, pick that.
You also have other minor issues in your code, like hard coding the qml path for Windows as showed below. You could change to either "main.qml" simply, or use the resource system.
view.setSource(QUrl("/Users/Qt/program/experiment_apps_and_libs/test/qml/test/main.qml"));
I personally changed it locally simply to:
view.setSource(QUrl("main.qml"));
Also, you seem to have unnecessary qmake options for this experiment set up as follows:
CONFIG += c++11
and
QT += qml quick
For the latter, you do not need to specify qml
explicitly.