Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct
QList of C++ custom objects to QML
Here is my need. I am reading an XML file in the C++ side of my application, and create some C++ objects of mine. After the reading, I send a signal to the QML to tell my GUI it has some QML components to dynamically instanciates.
I want those components to be initialized with the properties of the C++ objects I have created during the reading of the XML file.
So the idea here is to send a list of C++ objects to the QML, and parse that list inside the QML to create components.
I had a look at the QVariant class, but don't really know how to use it.
For example, I currently have a
QList<MyObjects*>that I want to expose to the QML via a Q_INVOKABLE method.
How do I do that? Does a
QList<QVariant*>will be enought?
How do I do that? Does a QList<QVariant*> will be enought?
Yes. But use
QList<QVariant>instead. When you registered the list contents (in the variant) to the meta type system (
Q_DECLARE_METATYPE) you can access it's QObject properties from QML.
Thanks for your answer.
To access the properties of the objects in the list, do I have to convert these properties to
Q_PROPERTYor is that unnecessary? With my understanding of QML, I would say that I have to.
With my understanding of QML, I would say that I have to.
exactly, since QML uses the meta type system.
Ok thanks for this additional explanation.
I'll give it a try and let you know if I face some difficulties.