Details
-
Suggestion
-
Resolution: Done
-
P2: Important
-
5.4.0
-
None
-
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
- relates to
-
QTBUG-43582 Enums declared in C++ base type
-
- Closed
-
For Gerrit Dashboard: QTBUG-43581 | ||||||
---|---|---|---|---|---|---|
# | Subject | Branch | Project | Status | CR | V |
103101,2 | WIP: Attached properties declared in C++ base type | dev | qt/qtdeclarative | Status: DEFERRED | 0 | 0 |
125599,3 | WIP: qmlRegisterType(): allow specifying the base type for composite types | 5.6 | qt/qtdeclarative | Status: ABANDONED | -2 | 0 |
125708,6 | Make QML composite types inherit attached properties | 5.6 | qt/qtdeclarative | Status: MERGED | -2 | 0 |
125713,5 | Attached properties are now available using the QML composite type name | 5.6 | qt/qtquickcontrols2 | Status: MERGED | +2 | 0 |