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: 250

    ListView {
        model: myModel
        anchors.fill: parent
    }
    

    }@

    The similar way in documentation doesnt lead to this error in output, but I dont 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 think you should use view->showNormal(); after setting context properties not before.



  • Ive tried this but it doesnt 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?





  • Thank you, it`s that I really need! :)


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.