Problem while usin QStringListModel in QML
-
Hello!
I`ve tried to use QStringListModel in both QtQuick 1 and 2, but have the same error in application output"file:///.../main.qml:14: ReferenceError: myModel is not defined"
My main.cpp file
@
...
QQuickView *view = new QQuickView;
view->setSource(QUrl::fromLocalFile("main.qml"));
view->showNormal();QStringListModel model; model.setStringList(list); //QStringList list is already initialized with 5 elements view->engine()->rootContext()->setContextProperty("myModel", &model);
...@
main.qml
@import QtQuick 2.0
Rectangle {
width: 420
height: 250ListView { model: myModel anchors.fill: parent }
}@
The similar way in documentation doesn
t lead to this error in output, but I don
t know how to display neither QQmlComponent nor QQuickItem
"QQmlContext documentation":http://doc-snapshot.qt-project.org/5.0/qtqml/qqmlcontext.html code example:
@QQmlEngine engine;
QStringListModel modelData;
QQmlContext *context = new QQmlContext(engine.rootContext());
context->setContextProperty("myModel", &modelData);QQmlComponent component(&engine);
component.setData("import QtQuick 2.0\nListView { model: myModel }", QUrl());
QObject *window = component.create(context);@ -
I
ve tried this but it doesn
t matter, i observed this error again :((
After a long time spent in qtDocumentation i`ve become confused with the mechanism of QML models delegates and views.
Does anybody know how to implement string list read from file and use it in my QML interface? -
It looks like you should simply use QStringList. Links here:
http://qt-project.org/doc/qt-4.8/qdeclarativemodels.html#c-data-models
http://qt-project.org/doc/qt-4.8/declarative-modelviews-stringlistmodel.html