Custom properties in QML
-
I will appreciate it if someone can give me a hint or two about this problem:
I have a QML component that receives some data from C++ code. The data is of format QList<QStringList>. From my C++ code, i need to pass this data to QML, process the data in the QML component and based on the number of QStringLists in the QList, i need to create ListViews and set their models to the respective QStringList i.e the number of views = the size of the QList
How do I declare a property of type QList<QStringList> in my QML code? This is the general idea:
@//C++ code
QList<QStringList> data;
QDeclarativeContext *ctxt = rootContext();
ctxt->setContextProperty("dataModel", QVariant::fromValue(data));
setSource(QUrl("qrc:/QML/test/main.qml"));//main.qml
Rectangle {
id: container
......
......
......
DateComponent {
dataModel:
}
}//DateComponent.qml
Item{
id: container
property dataModel
//parse dataModel and create required number of List views..
}@
-
With setContextProperty you can pass a QVariant or a QObject. QVariant can not hold a QList<QStringList>. You may try to put your data in a class derived from QObject. Another solution is to write a custom type as explained in qt4/examples/declarative/cppextensions/referenceexamples/adding
-
thanks. I've put my data in a class derived from QObject.