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. Passing a list of model from c++ to QML
Forum Update on Monday, May 27th 2025

Passing a list of model from c++ to QML

Scheduled Pinned Locked Moved QML and Qt Quick
6 Posts 3 Posters 7.6k 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.
  • L Offline
    L Offline
    luca
    wrote on last edited by
    #1

    Hi all,
    I have an array of model that I need to pass to QML to show in some listView .
    To pass a single model I use:
    @
    m_declarativeView->rootContext()->setContextProperty("riepilogoSatelliteModel", m_mappaRiepiloghiSatellitiModel[0]);
    @

    where m_mappaRiepiloghiSatellitiModel is:
    @
    QMap <int, RoleItemModel*>
    @
    and this works fine.

    Now I need to call something like this:
    @
    m_declarativeView->rootContext()->setContextProperty("riepilogoSatelliteModelArray", m_mappaRiepiloghiSatellitiModel.values());
    @
    to pass the whole array but Compilation fail:
    @
    ...
    error: no matching function for call to 'QDeclarativeContext::setContextProperty(const QString, QList<RoleItemModel*>)'
    ...
    @

    Is it possible to pass it to QML ?

    1 Reply Last reply
    0
    • A Offline
      A Offline
      andre
      wrote on last edited by
      #2

      Perhaps "this wiki page":http://developer.qt.nokia.com/wiki/How_to_expose_lists_to_QML is of use to you?

      1 Reply Last reply
      0
      • L Offline
        L Offline
        luca
        wrote on last edited by
        #3

        Now I tried with:
        @
        Q_DECLARE_METATYPE(QList<RoleItemModel*>)
        ...
        ...
        m_declarativeView->rootContext()->setContextProperty("riepilogoSatelliteModelList", QVariant::fromValue(m_mappaRiepiloghiSatellitiModel.values()));
        @

        and the compilation end without error.

        Now I don't know how to access the models list in QML. I'm trying with:
        @
        import QtQuick 1.0

        Rectangle {

        width: 400; height: 600
        
        ListView {
            id: lista_principale
            anchors.fill: parent
            model: riepilogoSatelliteModelList[0]
            orientation: ListView.Horizontal
            delegate: pagine_delegate
            snapMode: ListView.SnapToItem
        }
        

        ...
        ...
        @

        But when running I get:
        qrc:riepilogosatellite2.qml:10: ReferenceError: Can't find variable: riepilogoSatelliteModelList

        1 Reply Last reply
        0
        • L Offline
          L Offline
          lgeyer
          wrote on last edited by
          #4

          You will have to register both types to QML.
          @
          qmlRegisterType<QMap<int, RoleItemModel*> >();
          qmlRegisterType<RoleItemModel>();
          @
          However, you might get in trouble with the first one as QMap is not a QObject.

          1 Reply Last reply
          0
          • L Offline
            L Offline
            luca
            wrote on last edited by
            #5

            I thinks the problem is;
            @
            riepilogoSatelliteModelList[0]
            @

            because I've done some tests and it seems riepilogoSatelliteModelList is correctly exposed to QML but I don't know how to access a singole model in the list and use it in a ListView...

            1 Reply Last reply
            0
            • L Offline
              L Offline
              luca
              wrote on last edited by
              #6

              Ok, I solved by creating this class;
              @
              class MappaModel : public QObject
              {
              Q_OBJECT
              Q_PROPERTY(RoleItemModel* modelAttuale READ modelAttuale WRITE impostaModel)
              Q_PROPERTY(int chiaveAttuale READ chiaveAttuale WRITE impostaChiaveAttuale)

              public:
              explicit MappaModel(QObject parent = 0);
              void caricaMappa(QMap< int, RoleItemModel
              > );

              RoleItemModel *modelAttuale();
              void impostaModel(RoleItemModel *);
              
              void impostaChiaveAttuale(int indice);
              int chiaveAttuale();
              

              private:
              QMap< int, RoleItemModel* > m_mappaModel;
              int m_chiaveAttuale;

              signals:

              };
              @

              @
              #include "mappamodel.h"

              #include <QDebug>

              MappaModel::MappaModel(QObject *parent) :
              QObject(parent)
              {
              m_chiaveAttuale=7;
              }

              void MappaModel::caricaMappa(QMap<int, RoleItemModel *> mappa_model)
              {
              m_mappaModel = mappa_model;
              }

              //RoleItemModel* MappaModel::recuperaModel(int chiave)
              RoleItemModel* MappaModel::modelAttuale()
              {
              qDebug() << m_chiaveAttuale;
              qDebug() << m_mappaModel.value(m_chiaveAttuale);
              return m_mappaModel.value(m_chiaveAttuale);
              }

              void MappaModel::impostaModel(RoleItemModel *)
              {

              }

              void MappaModel::impostaChiaveAttuale(int chiave)
              {
              m_chiaveAttuale = chiave;
              }

              int MappaModel::chiaveAttuale()
              {
              return m_chiaveAttuale;
              }
              @

              Then in my mainWindow class:
              @
              qmlRegisterType<RoleItemModel>();
              ...
              ...
              ...
              m_declarativeView->rootContext()->setContextProperty("mappaModel", &m_mappaModel);
              @

              So my qml file is something like:
              @
              import QtQuick 1.0

              Rectangle {

              width: 400; height: 600
              
              ListView {
                  id: lista_principale
                  anchors.fill: parent
                  model: mappaModel.modelAttuale
                  orientation: ListView.Horizontal
                  delegate: pagine_delegate
                  snapMode: ListView.SnapToItem
              }
              

              ...
              ...
              @

              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