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

qml: deprecate top-level Components

    XMLWordPrintable

Details

    • d8066adc55 (qt/qtdeclarative/dev) d8066adc55 (qt/tqtc-qtdeclarative/dev)

    Description

      The idea behind a Component is to avoid creating a new file to declare a new type, and instead create it inline:

      Components are often defined by component files - that is, .qml files. The Component type essentially allows QML components to be defined inline, within a QML document, rather than as a separate QML file. This may be useful for reusing a small component within a QML file, or for defining a component that logically belongs with other QML components within a file.

      (from the docs at https://doc.qt.io/qt-6/qml-qtqml-component.html)

      It seems that qml currently supports top-level Components, which means that Component can also be used in a different file (instead of being used inline as the documentation advertises it for).

      Here an example:

      //ComponentType.qml
      import QtQml
      Component { // [1]
          id: componentRoot
          QtObject {
              objectName: "enclosed"
          }
      }
      
      //main.qml
      import QtQuick
      Item {
          ComponentType {
              id: myId // [2]
              property var myProperty // [3]
          }
      }
      

      List of headaches caused by this code:

      • [1]: Double Component: The qml file already denotes a component, using a component inside a component means that the component is "two times wrapped into a component" (via the implicit component wrapping). This component is completely useless but it allows even weirder uses because it enables the `ComponentType { /evil stuff here/}` notation (see [2] and [3]). Using Component not as toplevel element means that they cannot be used with the `{}` notation).
      • [2]: ids: this Component already has a name, the id is completely ignored in qml/qmlcachegen and is useless (cannot be seen or anywhere). Unfortunately, it makes the logic in qmltc more complicated (as it needs to know if the id is or is not in the compilation unit)
      • [3]: Forbidden properties: a Component only contains a name and one item, other stuff like properties/signal/methods are forbidden. But here the qml utility just ignores them.

      Subtasks:

      • reject top level Components in qmltc
      • deprecate top level components (maybe they can be removed in qt7)
        • also warn when finding top level components

      Attachments

        Issue Links

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

          Activity

            People

              sami.shalayel Sami Shalayel
              sami.shalayel Sami Shalayel
              Votes:
              0 Vote for this issue
              Watchers:
              2 Start watching this issue

              Dates

                Created:
                Updated:
                Resolved:

                Gerrit Reviews

                  There are no open Gerrit changes