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:
- m_declarativeView->rootContext()->setContextProperty("riepilogoSatelliteModel", m_mappaRiepiloghiSatellitiModel[0]);
where m_mappaRiepiloghiSatellitiModel is:
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 ?
5 replies
Perhaps this wiki page [developer.qt.nokia.com] is of use to you?
Now I tried with:
- ...
- ...
- 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
Ok, I solved by creating this class;
- {
- Q_OBJECT
- Q_PROPERTY(RoleItemModel* modelAttuale READ modelAttuale WRITE impostaModel)
- Q_PROPERTY(int chiaveAttuale READ chiaveAttuale WRITE impostaChiaveAttuale)
- public:
- RoleItemModel *modelAttuale();
- void impostaModel(RoleItemModel *);
- void impostaChiaveAttuale(int indice);
- int chiaveAttuale();
- private:
- int m_chiaveAttuale;
- signals:
- };
- #include "mappamodel.h"
- #include <QDebug>
- {
- m_chiaveAttuale=7;
- }
- {
- 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
- }
- ...
- ...
You must log in to post a reply. Not a member yet? Register here!




