Uploaded image for project: 'Qt'
  1. Qt
  2. QTBUG-113788

Documentation for QML-C++ Integration

    XMLWordPrintable

Details

    • Suggestion
    • Resolution: Unresolved
    • Not Evaluated
    • None
    • 6.5.0
    • Documentation
    • None
    • All

    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:

      1. 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
      2. 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
      3. 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

          No reviews matched the request. Check your Options in the drop-down menu of this sections header.

          Activity

            People

              docteam Qt Documentation Team
              pijaouen Pierre Jaouen
              Votes:
              0 Vote for this issue
              Watchers:
              2 Start watching this issue

              Dates

                Created:
                Updated:

                Gerrit Reviews

                  There are no open Gerrit changes