Details
-
Suggestion
-
Resolution: Unresolved
-
P3: Somewhat important
-
None
-
6.5.0
-
None
-
-
21
-
Foundation PM Prioritized, Foundation Sprint 108, Foundation Sprint 109, Foundation Sprint 110
Description
Hi,
I have several customers that ask about the best practices regarding QML-C++ integration.
I think the reason is that the documentation might benefit from getting clearer. Especially he would be nice to have a kind of summary with more detail than this one:
(Where are the context properties in the graph for instance? )
Basically if we start from here:
https://doc.qt.io/qt-6/qtqml-cppintegration-overview.html
we have 3 main methods proposed:
- The class can be registered as an instantiable QML type, so that it can be instantiated and used like any ordinary QML object type from QML code
- The class can be registered as a Singleton Type so that a single instance of the class may be imported from QML code, allowing the instance's properties, methods and signals to be accessed from QML
- An instance of the class can be embedded into QML code as a context property or context object, allowing the instance's properties, methods and signals to be accessed from QML
For each of them, some example of application, advantages-drawbacks could be explicitly mentioned (sorry if it is already done and I have just missed it)
1) Registering instantiable QML type: using QML_ELEMENT and qt_add_qml_module()
Why should be use QML_ELEMENT over qmlRegisterType()?
Advantages: Easy to use and implement, class properties available without the need of writing some methods
Drawbacks: no interface with C++?
Use Case: new QML component like a clock widget, component needs to be created several time
2) Singleton type: here too we have several macros and functions so it would be good to indicated which one is recommended now based on the use case (QML_SINGLETON, qmlRegisterSingletonInstance,...?)
Advantages: One access to all QML, simplify access to global properties
Drawbacks: Instance created by QQmlEngine so no control of the instantiation, need to be careful when doing threading (if using QML_SINGLETON)?
Use Case: model and view? logger, cache,...
3) Context property as documented here
https://doc.qt.io/qt-6/qtqml-cppintegration-contextproperties.html
Advantages: Easy to use and implement, instance in C++ so option for model-view
Drawbacks: unqualified access, potentially not optimized? deprecated?
Use Case: model and view for small projects
4) What about the other methods like QML_ANONYMOUS,... (again advantages, drawbacks, examples of uses cases and should we use the macro or functions could be mentioned.) Because some of them are in the chart but there are not mentioned above so it is not really clear...
Attachments
Issue Links
- relates to
-
QTBUG-113785 Warn about setContextProperty() in documentation
-
- Closed
-
Gerrit Reviews
For Gerrit Dashboard: QTBUG-113788 | ||||||
---|---|---|---|---|---|---|
# | Subject | Branch | Project | Status | CR | V |
563809,3 | Doc: Documentation for QML-C++ Integration | dev | qt/qtdeclarative | Status: NEW | 0 | 0 |