Qt6 QAbstractListModel - constructor with two arguments - qml Element is not creatable
-
Hey dear Qt forum,
I already made a post in stackoverflow, but I need some special Qt advise, I think:
stackoverflow: construcor with two elementsI want to create a graphical output with QML for a cpp particle system. Therefor I found out a construction, which worked for Qt5, but now errors with "qml object is not creatable": I use a QAbstractListModel Entrymodel class as wrapper for my System* class. So I made constructor for my Entrymodel, that has two arguments: QObject and System*. The argument System* pointer is thereby copied to System* pointer wrapped by the Entrymodel class. Otherwise the particle simulation would freeze at its starting configuration. But, with the two argument constructor, the resent state of the particle simulation is every times transferred to the next construction of the Entrymodel. There are parts of my code in the stackoverflow post.
However this worked pretty good with Qt5, but with Qt6 it errors. How can I make that work again?
Kind regards,
Tobias -
@Tobias83
The class has to be default-constructable, so the QML engine can create meta objects. Maybe you can solve that with a default argument. -
@GrecKo In the main function, I create an System object with argc, argv arguments and write this particle system to a parameters file. The constructor System() reads this parameters file by default when it is called by Qt. As I understood Qt right, there is a never ending loop between initiating a QObject with the QObject before. When I initiate the System every times new, it will always have its starting configuration. Therefor there is a continuous communication between the entrymodel and the qml object. But when I transfer it from one to the next QObject as pointer, I can make a continuous simulation. Or, what do you mean with multiple? And how does "singleton" work - I already read about that, but didn't understood it well.
-
@GrecKo There was some mistake in the previous answer ...
In the main function, I create an System object with argc, argv arguments and write this particle system to a parameters file. The constructor System() reads this parameters file by default when it is called by Qt. As I understood Qt right, there is a never ending loop between initiating a QObject with the QObject before. When I initiate the System every times new, it will always have its starting configuration. But when I transfer it from one to the next QObject as pointer, I can make a continuous simulation. Therefor there is a continuous communication between the entrymodel and the qml object. Or, what do you mean with multiple? And how does "singleton" work - I already read about that, but didn't understood it well. -
-
@Axel-Spoerl said in Qt6 QAbstractListModel - constructor with two arguments - qml Element is not creatable:
@Tobias83
https://doc.qt.io/QtForMCUs-2.5/qtul-qml-singleton.htmlNot relevant to the needs of Tobias83. We are not talking about an object defined in QML.
@Tobias83 said in Qt6 QAbstractListModel - constructor with two arguments - qml Element is not creatable:
@GrecKo Is there a good howto for singletons? I didn't found one ...
The official ones are not that practical I'm afraid: https://doc.qt.io/qt-6/qqmlengine.html#QML_SINGLETON
To expose a business class I would recommend the last example.To reduce the boiler plate I've wrote some macros wrapping the above and Ekke a blog post explaining the context and how they can be used: https://ekkesapps.wordpress.com/qt-6-in-action/qmake-cmake/qml_singleton/
-
@GrecKo Dear GrecKo,
in the qml_singleton.h should I change the word "Type" to "System", because my Particlesystem is called like that?
Where have I to introduce my Hardcore_Entrymodel - is it the UnsafeArea or is it the ApplicationUI class?
I tend to UnsafeArea=Hardcore_Entrymodel and to leave the names "ApplicationUI" and "Type##QmlSingleton", but I'm not sure ... -