October 28, 2011

Luca Luca
Ant Farmer
589 posts

Passing a list of model from c++ to QML

 

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:

  1. m_declarativeView->rootContext()->setContextProperty("riepilogoSatelliteModel",  m_mappaRiepiloghiSatellitiModel[0]);

where m_mappaRiepiloghiSatellitiModel is:

  1.  
  2. QMap <int, RoleItemModel*>

and this works fine.

Now I need to call something like this:

  1. m_declarativeView->rootContext()->setContextProperty("riepilogoSatelliteModelArray",  m_mappaRiepiloghiSatellitiModel.values());

to pass the whole array but Compilation fail:
  1. ...
  2. error: no matching function for call to 'QDeclarativeContext::setContextProperty(const QString, QList<RoleItemModel*>)'
  3. ...

Is it possible to pass it to QML ?

5 replies

October 28, 2011

Andre Andre
Area 51 Engineer
6031 posts

Perhaps this wiki page [developer.qt.nokia.com] is of use to you?

 Signature 

Looking for Qt developers to join our team @ i-Optics: https://qt-project.org/forums/viewthread/25393/

October 28, 2011

Luca Luca
Ant Farmer
589 posts

Now I tried with:

  1. Q_DECLARE_METATYPE(QList<RoleItemModel*>)
  2. ...
  3. ...
  4. 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:

  1. import QtQuick 1.0
  2.  
  3. Rectangle {
  4.  
  5.     width: 400; height: 600
  6.  
  7.     ListView {
  8.         id: lista_principale
  9.         anchors.fill: parent
  10.         model: riepilogoSatelliteModelList[0]
  11.         orientation: ListView.Horizontal
  12.         delegate: pagine_delegate
  13.         snapMode: ListView.SnapToItem
  14.     }
  15. ...
  16. ...

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

October 28, 2011

Lukas Geyer Lukas Geyer
Gene Splicer
2074 posts

You will have to register both types to QML.

  1. qmlRegisterType<QMap<int, RoleItemModel*> >();
  2. qmlRegisterType<RoleItemModel>();

However, you might get in trouble with the first one as QMap is not a QObject.

October 28, 2011

Luca Luca
Ant Farmer
589 posts

I thinks the problem is;

  1. 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…

October 28, 2011

Luca Luca
Ant Farmer
589 posts

Ok, I solved by creating this class;

  1. class MappaModel : public QObject
  2. {
  3.     Q_OBJECT
  4.     Q_PROPERTY(RoleItemModel* modelAttuale READ modelAttuale WRITE impostaModel)
  5.     Q_PROPERTY(int chiaveAttuale READ chiaveAttuale WRITE impostaChiaveAttuale)
  6.  
  7. public:
  8.     explicit MappaModel(QObject *parent = 0);
  9.     void caricaMappa(QMap< int, RoleItemModel* > );
  10.  
  11.     RoleItemModel *modelAttuale();
  12.     void impostaModel(RoleItemModel *);
  13.  
  14.     void impostaChiaveAttuale(int indice);
  15.     int chiaveAttuale();
  16.  
  17. private:
  18.     QMap< int, RoleItemModel* > m_mappaModel;
  19.     int m_chiaveAttuale;
  20.  
  21. signals:
  22.  
  23. };

  1. #include "mappamodel.h"
  2.  
  3. #include <QDebug>
  4.  
  5. MappaModel::MappaModel(QObject *parent) :
  6.     QObject(parent)
  7. {
  8.     m_chiaveAttuale=7;
  9. }
  10.  
  11. void MappaModel::caricaMappa(QMap<int, RoleItemModel *> mappa_model)
  12. {
  13.     m_mappaModel = mappa_model;
  14. }
  15.  
  16. //RoleItemModel* MappaModel::recuperaModel(int chiave)
  17. RoleItemModel* MappaModel::modelAttuale()
  18. {
  19.     qDebug() << m_chiaveAttuale;
  20.     qDebug() << m_mappaModel.value(m_chiaveAttuale);
  21.     return m_mappaModel.value(m_chiaveAttuale);
  22. }
  23.  
  24. void MappaModel::impostaModel(RoleItemModel *)
  25. {
  26.  
  27. }
  28.  
  29. void MappaModel::impostaChiaveAttuale(int chiave)
  30. {
  31.     m_chiaveAttuale = chiave;
  32. }
  33.  
  34. int MappaModel::chiaveAttuale()
  35. {
  36.     return m_chiaveAttuale;
  37. }

Then in my mainWindow class:

  1. qmlRegisterType<RoleItemModel>();
  2. ...
  3. ...
  4. ...
  5. m_declarativeView->rootContext()->setContextProperty("mappaModel", &m_mappaModel);

So my qml file is something like:

  1. import QtQuick 1.0
  2.  
  3. Rectangle {
  4.  
  5.     width: 400; height: 600
  6.  
  7.     ListView {
  8.         id: lista_principale
  9.         anchors.fill: parent
  10.         model: mappaModel.modelAttuale
  11.         orientation: ListView.Horizontal
  12.         delegate: pagine_delegate
  13.         snapMode: ListView.SnapToItem
  14.     }
  15. ...
  16. ...

 
  ‹‹ How to sort items in listview in qml      QDeclarativeView3D black screen while loading 3D model ››

You must log in to post a reply. Not a member yet? Register here!