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. [ListView]How to use C++ to add Row for ListView?
Forum Updated to NodeBB v4.3 + New Features

[ListView]How to use C++ to add Row for ListView?

Scheduled Pinned Locked Moved QML and Qt Quick
2 Posts 2 Posters 1.1k Views 1 Watching
  • 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.
  • B Offline
    B Offline
    BBFAG3
    wrote on 15 Dec 2013, 14:24 last edited by
    #1

    I have no idea about it.. I tried to set ListView's model as a QStringList, but it doesn't update when I append string to QStringList...
    Thanks a lot...

    1 Reply Last reply
    0
    • V Offline
      V Offline
      Vincent007
      wrote on 15 Dec 2013, 15:15 last edited by
      #2

      There is an unofficial way.

      use QMetaObject::invokeMethod

      @
      QQmlApplicationEngine engine(QUrl("qrc:/qml/main.qml"));
      QObject *topLevel = engine.rootObjects().value(0);
      QQuickWindow *window = qobject_cast<QQuickWindow *>(topLevel);
      if ( !window ) {
      qWarning("Error: Your root item has to be a Window.");
      return -1;
      }
      window->show();
      QObject model = topLevel->findChild<QObject>("listmodel");
      if (model) {
      qDebug() << "model exists";
      QVariantMap newElement; // QVariantMap will implicitly translates into JS-object
      newElement.insert("name","from cpp");
      QMetaObject::invokeMethod(
      model, // for this object we will call method
      "append_", // actually, name of the method to call
      Q_ARG(QVariant, QVariant::fromValue(newElement)) // method parameter
      );
      }
      @
      main.qml contains
      @
      ListView {
      id: list_view1
      anchors.fill: parent
      delegate: Item {
      x: 5
      height: 40
      Row {
      id: row1
      spacing: 10
      Text {
      text: name
      font.bold: true
      anchors.verticalCenter: parent.verticalCenter
      }
      }
      }
      model: ListModel {
      id: myModel
      objectName: "listmodel"
      function append_(newElement) {
      append(newElement)
      }
      }
      }@

      1 Reply Last reply
      0

      1/2

      15 Dec 2013, 14:24

      • Login

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