[Qt-qml] Multiple declarative components and a single ListModel

Diego Marcos diego.marcos at gmail.com
Tue Jul 13 13:05:56 CEST 2010


Hi there!!,

I´m combining multiple GUI elements based on QML (embedded in a QGraphicsLayoutItem) with other "traditional" QGraphicsWidgets in a single QGraphicsGridLayout. Everything worked smoothly except one thing.

I want to have access to a single data model that will be displayed and modified through multiple of my QML components. I instantiated one declarative context and engine shared by all the GUI elements.

In the C++ code I can do the following and it works as expected:

 QDeclarativeContext* qmlContext = qmlEngine->rootContext();
 qmlContext->setContextProperty("testString", QString("TEST"));

The string is available in all my QML components. 

What should I do to expose a single data model for all components? No success for the moment. There's obviously something I´m not understanding well:

Approach 1:

QDeclarativeComponent* modelComponent = new QDeclarativeComponent(qmlEngine,  QUrl("qrc:/declarative/DataModels.qml"));
modelComponent->create(qmlContext);

Approach 2:

QDeclarativeComponent* modelComponent = new QDeclarativeComponent(qmlEngine, QUrl("qrc:/declarative/DataModels.qml"));
qmlContext->setContextProperty("layersModel", modelComponent);

DataModels.qml
----------------------

import Qt 4.7

ListModel {
    id: layersModel
}

Thank you very much,

Diego.



More information about the Qt-qml mailing list