View Full Version : Access to model properties in QML when lists are created in C++

8th February 2011, 20:30

I am using qmlifyproxy library (http://www.qtcentre.org/wiki/index.php?title=QMLifyProxyModel) to call a model written in C++ that displays a Grid of images. Until now images are displayed fine.
My problem is: I cannot give to the "model: MemoryModel" sentence in the code piece below an id property visible for QML to enable access to the model properties (in my case I need to use "get" and "move" properties as detailed in the main.qml code).

when I write the model call sentence in the code below with:
model: memoryModel
there are no errors obviously, but it does not work because I cannot access the properties

But, the code below:
with the sentence:
model: memoryModel { id: icons }
to enable access to use the properties, I get the following error:
file:///../SQLListViewProxy-build-desktop/qml/SQLListViewProxy/main.qml:15:14: Expected type name model: memoryModel { id: icons}

and I can not continue...

To clarify, my question is: How is it possible in a model created in C++ to give an id property visible from QML to enable access to the model properties as "get" or "move"?

main.cpp where "memoryModel" is referred is attached.
memoryitem.qml to provide more context info, also.



8th February 2011, 22:15
Please try to explain your problem more clearly. What is the "id" property and what is it used for? "model" should state an identifier bound to a data model, nothing else. If you wish to set the "id" property of your model to "icons" then you need to run the following javascript expression somewhere:
memoryModel.id = "icons"

But this 'somewhere' can't be the statement binding a model to your view, the syntax is simply wrong.

9th February 2011, 10:18
Thanks for answering,
Let me clarify, of course:
1) From a C++ tabular list model (composed of strings: paths to files of images and descriptor texts) it is created a list model declared as "memoryModel" in QML's GridView. (See main.cpp and main_new.qml)
2) GridView displays images and texts properly
3) a QML ListModel needs an id so all members of ListModel can be referenced(for example "get" and "move"). (See main_new.qml)
4) I have declared the sentence "model: memoryModel { id: icons }" in main.qml to enable usage of ListModel members icons.get and icons.move

The issue is: the sentence above gets a compiler error. As a consequence, I do not know how to define a valid id (as "icons" in the code) to use ListModel members in QML.
The reason is that memoryModel is not ListModel declared in QML but imported from C++, probably, and I do not know how to define a valid id in this case to enable usage of ListModel members in QML.

Hope it helps,


9th February 2011, 11:07
The id of the model is the name of the property you export from within C++ using QDeclarativeContext. You can of course provide an alias to it using regular QML means.

QDeclarativeContext *context = view.rootContext();
context->setContextProperty("FileModel", &proxy); // "FileModel" is the id of the model

In the example above "FileModel" represents an object and not a type. In your code "MemoryModel" is the id of your model object. If you want to have more memory models you need to export each and every one in C++ using different property names.