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:
    @
    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 ?



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



  • 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



  • 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.



  • 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...



  • 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
    }
    

    ...
    ...
    @


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.