Attached property VS Q_EXTENDED: add custom properties to a general QML Control Item
-
Dear all,
I'm looking for add properties to a generic QML Control Item
Supposing I have a class similar to this one:
class ExtraProperties : public QObject { Q_OBJECT public: Q_PROPERTY(QString placeholderText READ getPlaceholderText WRITE setPlaceholderText NOTIFY placeholderTextChanged) Q_PROPERTY(QString helperText READ getHelperText WRITE setHelperText NOTIFY helperTextChanged) private: QString placeholderText; QString helperText; public: explicit ExtraProperties(QObject* parent = nullptr); QString getPlaceholderText() const; void setPlaceholderText(const QString& newPlaceholderText); QString getHelperText() const; void setHelperText(const QString& newHelperText); signals: void placeholderTextChanged(); void helperTextChanged(); };
I'd like to adding these two properties (
placeholderText
andhelperText
) to some QML components such as:- TextEdit
- SpinBox
- ecc..
In Qt docs, I see there are mainly two possibilities to reach this target:
- using Q_EXTEND
This is the solution I'd prefer, however this isn't feasible because I haven't access to QQuickTextEdit headers, because they are private; so this code couldn't work (following this example)
struct QLineEditForeign { Q_GADGET QML_FOREIGN(QQuickTextEdit) <--- KO, no headers to QuickTextEdit QML_NAMED_ELEMENT(TextEdit) QML_EXTENDED(ExtraProperties) };
- using attached-properties
The problem of attaching properties is that I'm not able to use them outside of item, for example:
class ExtraPropertiesAtt : public QObject { Q_OBJECT QML_ATTACHED(ExtraProperties) QML_ELEMENT public: static ExtraProperties* qmlAttachedProperties(QObject *object) { return new ExtraProperties(object); } };
From QML then
- CustomItem.qml
Item { ... ExtraPropertiesAtt.placeholderText: "Sample" ... }
- Main.qml
import CustomItem Window { CustomItem { ExtraPropertiesAtt.placeholderText: "Test" <--- KO } }
So, are there any other ways to achieve this goal?
Thanks,
Nicola -
@nico88desmo How does the "KO" occur in your last snippet?
-
@GrecKo from my first test code, it seems that CustomItem's
placeholderText
value is set equals to Item'splaceholderText
, ignoring the assignment in Main.qml.That's why I wrote KO; it's quite strange... unless I'm doing some other errors in other code parts.
-
@nico88desmo said in Attached property VS Q_EXTENDED: add custom properties to a general QML Control Item:
@GrecKo from my first test code, it seems that CustomItem's
placeholderText
value is set equals to Item'splaceholderText
, ignoring the assignment in Main.qml.That's why I wrote KO; it's quite strange... unless I'm doing some other errors in other code parts.
@GrecKo I've rechecked the code, and effectively the problem was that in my real code (I've posted just a snippet in order to no put boilerplate code inside forum page),
Item
is inside a plugin andMain.qml
is in the main application.The problem was quite stupid: plugin wasn't recompiled so
Main.qml
uses an oldItem
's version.Now attached property works as expected
-