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. Make stringlistmodel example workl with ListView not being the top level item
QtWS25 Last Chance

Make stringlistmodel example workl with ListView not being the top level item

Scheduled Pinned Locked Moved Solved QML and Qt Quick
2 Posts 1 Posters 175 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.
  • H Offline
    H Offline
    HJPVVV
    wrote on last edited by
    #1

    I would love to see this example stringlistmodel work with the ListView not being the top level item.
    The C++ code sets the initial property "model" of QQuickView. This does not work if the ListView is wrapped in e.g. some layout item. How could I make this work?

    1 Reply Last reply
    0
    • H Offline
      H Offline
      HJPVVV
      wrote on last edited by HJPVVV
      #2

      I managed to find out myself. Here is the code, in case others might want the same:

      C++

      #include <QGuiApplication>
      #include <QQmlApplicationEngine>
      #include <QStringList>
      
      #include <qqmlengine.h>
      #include <qqmlcontext.h>
      #include <qqml.h>
      #include <QtQuick/qquickitem.h>
      #include <QtQuick/qquickview.h>
      
      int main(int argc, char ** argv)
      {
          QGuiApplication app(argc, argv);
      
          QQmlApplicationEngine engine;
          const QUrl url(QStringLiteral("qrc:/view.qml"));
          QObject::connect(
                     &engine,
                     &QQmlApplicationEngine::objectCreated,
                     &app,
                     [url](QObject *obj, const QUrl &objUrl) {if (!obj && url == objUrl) QCoreApplication::exit(-1);}, Qt::QueuedConnection);
          engine.load(url);
      
          //  get the root object of the loaded QML
          QList<QObject*> root_objects(engine.rootObjects());
      
          QStringList projectList = {
              "Project 1",
              "Project 2",
              "Project 3",
              "Project 4"
          };
      
          const QObjectList& children(root_objects[0]->children());
          if (!children.isEmpty())
          {
              QList<QObject*> lists(root_objects[0]->findChildren<QObject*>("listViewProjects"));
              if (!lists.isEmpty())
                  lists.first()->setProperty("model", QVariant::fromValue(projectList));
          }
      
          return app.exec();
      }
      
      
      

      QML

      
      import QtQuick 2.4
      import QtQuick 2.15
      import QtQuick.Controls 2.15
      import QtQuick.Window 2.5
      
      Window {
          id: window
          visible: true
          width: 640
          height: 480
          title: qsTr("ListView")
      
          ListView {
              id: listViewProjects
              objectName: "listViewProjects"
              anchors.fill: parent
      
              delegate: Item {
                  id: project
                  required property string modelData
                  required property int index
                  height: 30
                  width: parent.width
                  Text {
                      anchors.verticalCenter: parent.verticalCenter
                      text: parent.modelData
                  }
                  MouseArea {
                      anchors.fill: parent
                      onClicked: listViewProjects.currentIndex = parent.index
                  }
              }
              highlight: Rectangle { color: "lightsteelblue"; radius: 2 }
              focus: true
          }
      }
      
      

      It also shows how to use the index variable for setting the selection with the mouse: once a required property is declared (modelData), you must declare all properties you intend to use. This is not shown in any other example I could find. If you fail to do this, index is undefined.

      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