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

Attached properties declared in C++ base type

    XMLWordPrintable

Details

    • 8e7d1a91196197eee4e45bbfa9886ab935e2b67c

    Description

      A common problem for controls is that when the business logic is implemented in a C++ "base" type and the visual parts in a QML "sub" type, attached properties provided by the C++ type (hidden implementation detail) cannot be accessed by the public QML type name.

      Example: a generic ScrollIndicator that can be attached to any Flickable.

      C++ base type:

      // qquickabstractscrollindicator_p.h
      class QQuickScrollIndicatorAttached : public QObject
      {
          Q_OBJECT
          Q_PROPERTY(QQuickAbstractScrollIndicator *horizontal ...)
          Q_PROPERTY(QQuickAbstractScrollIndicator *vertical ...)
      };
      
      class QQuickAbstractScrollIndicator : public QQuickItem
      {
          Q_OBJECT
      
      public:
          explicit QQuickAbstractScrollIndicator(QQuickItem *parent = Q_NULLPTR);
      
          static QQuickScrollIndicatorAttached *qmlAttachedProperties(QObject *object);
      };
      
      QML_DECLARE_TYPEINFO(QQuickAbstractScrollIndicator, QML_HAS_ATTACHED_PROPERTIES)
      
      // qtquickcontrolsplugin.cpp
      qmlRegisterType<QQuickAbstractScrollIndicator>(uri, 2, 0, "AbstractScrollIndicator");
      

      QML type:

      // qmldir
      ScrollIndicator 2.0 ScrollIndicator.qml
      
      // ScrollIndicator.qml
      AbstractScrollIndicator {
          id: control
          implicitWidth: ...
          implicitHeight: ...
          indicator: Rectangle { ... }
          background: Rectangle { ... }
      }
      

      Application code cannot use ScrollIndicator.vertical (QML engine throws "Non-existent attached object" error), but the attached property must be referenced using the C++ base type name ie. AbstractScrollIndicator. This makes naming very difficult to achieve optimal API for such controls.

      Flickable {
          // ScrollIndicator.vertical: ScrollIndicator { ... } // "Non-existent attached object"
          // vs.
          AbstractScrollIndicator.vertical: ScrollIndicator { ... } // :(
      }
      

      Attachments

        Issue Links

          For Gerrit Dashboard: QTBUG-43581
          # Subject Branch Project Status CR V

          Activity

            People

              Unassigned Unassigned
              jpnurmi J-P Nurmi
              Votes:
              3 Vote for this issue
              Watchers:
              4 Start watching this issue

              Dates

                Created:
                Updated:
                Resolved:

                Gerrit Reviews

                  There are no open Gerrit changes