Qt Forum

    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Unsolved

    Problem while usin QStringListModel in QML

    QML and Qt Quick
    2
    5
    5404
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • W
      wild34 last edited by

      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);@

      1 Reply Last reply Reply Quote 0
      • D
        daliusd last edited by

        I think you should use view->showNormal(); after setting context properties not before.

        1 Reply Last reply Reply Quote 0
        • W
          wild34 last edited by

          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?

          1 Reply Last reply Reply Quote 0
          • D
            daliusd last edited by

            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

            1 Reply Last reply Reply Quote 0
            • W
              wild34 last edited by

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

              1 Reply Last reply Reply Quote 0
              • First post
                Last post