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

Attached properties declared in C++ base type

    Details

    • Commits:
      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

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

            Activity

              People

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

                Dates

                • Created:
                  Updated:
                  Resolved:

                  Gerrit Reviews Side Panel

                  Error rendering 'com.meetme.plugins.jira.gerrit-plugin:gerrit-reviews-side-panel'. Please contact your Jira administrators.