Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. QML and Qt Quick
  4. Problem while usin QStringListModel in QML
QtWS25 Last Chance

Problem while usin QStringListModel in QML

Scheduled Pinned Locked Moved QML and Qt Quick
5 Posts 2 Posters 5.7k Views
  • 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 Offline
    W Offline
    wild34
    wrote on last edited by
    #1

    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
    0
    • D Offline
      D Offline
      daliusd
      wrote on last edited by
      #2

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

      1 Reply Last reply
      0
      • W Offline
        W Offline
        wild34
        wrote on last edited by
        #3

        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
        0
        • D Offline
          D Offline
          daliusd
          wrote on last edited by
          #4

          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
          0
          • W Offline
            W Offline
            wild34
            wrote on last edited by
            #5

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

            1 Reply Last reply
            0

            • Login

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • Users
            • Groups
            • Search
            • Get Qt Extensions
            • Unsolved