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. How to display a QAbstractListModel in qml?
Forum Updated to NodeBB v4.3 + New Features

How to display a QAbstractListModel in qml?

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

    I want to display a QAbstractListModel in qml. I create a simple app but fails to make it work. The window is empty and the model is not accessed from qml. Please help point out what's wrong with this code.

    // main.cpp
    #include <QAbstractListModel>
    #include <QGuiApplication>
    #include <QQmlApplicationEngine>
    #include <QQmlContext>
    
    struct Model : QAbstractListModel {
      QStringList lst{"txt1", "txt2", "txt3"};
    
      int rowCount(const QModelIndex &parent) const override {
        // never called
        if (parent.isValid())
          return 0;
        return lst.length();
      }
      QVariant data(const QModelIndex &index, int role) const override {
        // never called
        if (index.row() < 0 || index.row() >= lst.size())
          return QVariant{};
        if (role == Qt::DisplayRole)
          return lst.at(index.row());
        return QVariant{};
      }
    };
    
    int main(int argc, char *argv[]) {
      QGuiApplication app{argc, argv};
      Model model;
      QQmlApplicationEngine engine;
      QObject::connect(
          &engine, &QQmlApplicationEngine::objectCreationFailed, &app,
          []() { QCoreApplication::exit(-1); }, Qt::QueuedConnection);
      QQmlContext *context = engine.rootContext();
      context->setContextProperty("model", &model);
      // doesn't work: engine.setInitialProperties({{"model",
      // QVariant::fromValue(&model)}});
    
      engine.loadFromModule("app", "Main");
      return app.exec();
    }
    
    import QtQuick
    import QtQuick.Controls
    ApplicationWindow {
        visible: true
        width: 640
        height: 480
        title: "App"
    
        ListView {
            id: listView
            anchors.fill: parent
            model: model
    
            delegate: Item {
                width: listView.width
                height: 50
    
                Text {
                    anchors.centerIn: parent
                    text: model.display
                }
            }
        }
    }
    
    jeremy_kJ 1 Reply Last reply
    0
    • jeremy_kJ jeremy_k

      @mjs225 said in How to display a QAbstractListModel in qml?:

      I want to display a QAbstractListModel in qml. I create a simple app but fails to make it work. The window is empty and the model is not accessed from qml. Please help point out what's wrong with this code.

      // main.cpp
        // doesn't work: engine.setInitialProperties({{"model",
        // QVariant::fromValue(&model)}});
      

      The top level component doesn't have a model property, and setInitialProperties doesn't recursively search subcomponents to apply to.

      ApplicationWindow {
      
          ListView {
              id: listView
              anchors.fill: parent
              model: model
          }
      }
      

      The right side of model: model within listView is going to resolve to listView.model, ie itself.

      M Offline
      M Offline
      mjs225
      wrote on last edited by
      #3

      @jeremy_k said in How to display a QAbstractListModel in qml?:

      @mjs225 said in How to display a QAbstractListModel in qml?:

      I want to display a QAbstractListModel in qml. I create a simple app but fails to make it work. The window is empty and the model is not accessed from qml. Please help point out what's wrong with this code.

      // main.cpp
        // doesn't work: engine.setInitialProperties({{"model",
        // QVariant::fromValue(&model)}});
      

      The top level component doesn't have a model property, and setInitialProperties doesn't recursively search subcomponents to apply to.

      ApplicationWindow {
      
          ListView {
              id: listView
              anchors.fill: parent
              model: model
          }
      }
      

      The right side of model: model within listView is going to resolve to listView.model, ie itself.

      Thanks. I find 2 fixes based on the suggestion:

      • Rename the property "model".
        in C++: context->setContextProperty("mymodel", &model);
        in qml: ListView { model: mymodel }
      • in C++: replace context->setContextProperty("model", &model); with engine.setInitialProperties({{"model", QVariant::fromValue(&model)}});
        in qml: Add property var model and id: appWindow to top level component. Use model: appWindow.model in ListView
      1 Reply Last reply
      0
      • M mjs225

        I want to display a QAbstractListModel in qml. I create a simple app but fails to make it work. The window is empty and the model is not accessed from qml. Please help point out what's wrong with this code.

        // main.cpp
        #include <QAbstractListModel>
        #include <QGuiApplication>
        #include <QQmlApplicationEngine>
        #include <QQmlContext>
        
        struct Model : QAbstractListModel {
          QStringList lst{"txt1", "txt2", "txt3"};
        
          int rowCount(const QModelIndex &parent) const override {
            // never called
            if (parent.isValid())
              return 0;
            return lst.length();
          }
          QVariant data(const QModelIndex &index, int role) const override {
            // never called
            if (index.row() < 0 || index.row() >= lst.size())
              return QVariant{};
            if (role == Qt::DisplayRole)
              return lst.at(index.row());
            return QVariant{};
          }
        };
        
        int main(int argc, char *argv[]) {
          QGuiApplication app{argc, argv};
          Model model;
          QQmlApplicationEngine engine;
          QObject::connect(
              &engine, &QQmlApplicationEngine::objectCreationFailed, &app,
              []() { QCoreApplication::exit(-1); }, Qt::QueuedConnection);
          QQmlContext *context = engine.rootContext();
          context->setContextProperty("model", &model);
          // doesn't work: engine.setInitialProperties({{"model",
          // QVariant::fromValue(&model)}});
        
          engine.loadFromModule("app", "Main");
          return app.exec();
        }
        
        import QtQuick
        import QtQuick.Controls
        ApplicationWindow {
            visible: true
            width: 640
            height: 480
            title: "App"
        
            ListView {
                id: listView
                anchors.fill: parent
                model: model
        
                delegate: Item {
                    width: listView.width
                    height: 50
        
                    Text {
                        anchors.centerIn: parent
                        text: model.display
                    }
                }
            }
        }
        
        jeremy_kJ Offline
        jeremy_kJ Offline
        jeremy_k
        wrote on last edited by
        #2

        @mjs225 said in How to display a QAbstractListModel in qml?:

        I want to display a QAbstractListModel in qml. I create a simple app but fails to make it work. The window is empty and the model is not accessed from qml. Please help point out what's wrong with this code.

        // main.cpp
          // doesn't work: engine.setInitialProperties({{"model",
          // QVariant::fromValue(&model)}});
        

        The top level component doesn't have a model property, and setInitialProperties doesn't recursively search subcomponents to apply to.

        ApplicationWindow {
        
            ListView {
                id: listView
                anchors.fill: parent
                model: model
            }
        }
        

        The right side of model: model within listView is going to resolve to listView.model, ie itself.

        Asking a question about code? http://eel.is/iso-c++/testcase/

        M 1 Reply Last reply
        0
        • jeremy_kJ jeremy_k

          @mjs225 said in How to display a QAbstractListModel in qml?:

          I want to display a QAbstractListModel in qml. I create a simple app but fails to make it work. The window is empty and the model is not accessed from qml. Please help point out what's wrong with this code.

          // main.cpp
            // doesn't work: engine.setInitialProperties({{"model",
            // QVariant::fromValue(&model)}});
          

          The top level component doesn't have a model property, and setInitialProperties doesn't recursively search subcomponents to apply to.

          ApplicationWindow {
          
              ListView {
                  id: listView
                  anchors.fill: parent
                  model: model
              }
          }
          

          The right side of model: model within listView is going to resolve to listView.model, ie itself.

          M Offline
          M Offline
          mjs225
          wrote on last edited by
          #3

          @jeremy_k said in How to display a QAbstractListModel in qml?:

          @mjs225 said in How to display a QAbstractListModel in qml?:

          I want to display a QAbstractListModel in qml. I create a simple app but fails to make it work. The window is empty and the model is not accessed from qml. Please help point out what's wrong with this code.

          // main.cpp
            // doesn't work: engine.setInitialProperties({{"model",
            // QVariant::fromValue(&model)}});
          

          The top level component doesn't have a model property, and setInitialProperties doesn't recursively search subcomponents to apply to.

          ApplicationWindow {
          
              ListView {
                  id: listView
                  anchors.fill: parent
                  model: model
              }
          }
          

          The right side of model: model within listView is going to resolve to listView.model, ie itself.

          Thanks. I find 2 fixes based on the suggestion:

          • Rename the property "model".
            in C++: context->setContextProperty("mymodel", &model);
            in qml: ListView { model: mymodel }
          • in C++: replace context->setContextProperty("model", &model); with engine.setInitialProperties({{"model", QVariant::fromValue(&model)}});
            in qml: Add property var model and id: appWindow to top level component. Use model: appWindow.model in ListView
          1 Reply Last reply
          0
          • M mjs225 has marked this topic as solved on

          • Login

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