What is Q_PROPERTY's equivalent to QML default properties?
-
I have found the "default" attribute for properties defined in QML.
I would like to make a property of a C++ Q_GADGET a default property, but I can't seem to find the right keyword in Q_PROPERTY.
-
I have found the "default" attribute for properties defined in QML.
I would like to make a property of a C++ Q_GADGET a default property, but I can't seem to find the right keyword in Q_PROPERTY.
It's
Q_CLASSINFO("DefaultProperty", "propertyname")See e.g. https://doc.qt.io/qt-6.2/qtqml-referenceexamples-default-example.html
-
It's
Q_CLASSINFO("DefaultProperty", "propertyname")See e.g. https://doc.qt.io/qt-6.2/qtqml-referenceexamples-default-example.html
@kkoehne Wow, that's really hidden in an obscure place in the docs...
Thanks a lot! -
A Asperamanca has marked this topic as solved on
-
@kkoehne Wow, that's really hidden in an obscure place in the docs...
Thanks a lot!@Asperamanca To be fair, it's also explained in
https://doc.qt.io/qt-6/qtqml-cppintegration-definetypes.html
Where did you look for this information? Just so that we can add some cross-references ...
-
@Asperamanca To be fair, it's also explained in
https://doc.qt.io/qt-6/qtqml-cppintegration-definetypes.html
Where did you look for this information? Just so that we can add some cross-references ...
@kkoehne
I looked in the explanation of the Qt Property system, and searched the whole page for "default". I also looked for links to further information especially around the section describing Q_PROPERTY -
@kkoehne
I looked in the explanation of the Qt Property system, and searched the whole page for "default". I also looked for links to further information especially around the section describing Q_PROPERTY@Asperamanca But I see, this default property is a QML-specific thing, so it doesn't belong into the general explanation of the Qt Property System. Maybe a link to the QML-specific parts would help.
-
@Asperamanca But I see, this default property is a QML-specific thing, so it doesn't belong into the general explanation of the Qt Property System. Maybe a link to the QML-specific parts would help.
@Asperamanca Created https://codereview.qt-project.org/c/qt/qtbase/+/526494 for now. let's see.
-
@Asperamanca Created https://codereview.qt-project.org/c/qt/qtbase/+/526494 for now. let's see.
@kkoehne However, does this even work for properties of value types?
I have something like
namespace MyModule { class IntWrapper { Q_GADGET QML_VALUE_TYPE(myModule_IntWrapper) Q_PROPERTY(int intValue READ getIntValue WRITE setIntValue) Q_CLASSINFO("DefaultProperty", "intValue") public: int m_Value = {}; int getValue() const; void setValue(const int& arg); }; } // namespace MyModuleAnd in QML:
import MyModule; ComboBox { readonly property myModule_IntWrapper testInt: highlightedIndex }I would expect testInt to be initialized with the highlightedIndex, but instead I get
Unable to assign int to MyModule::IntWrapper.I can do this:
ComboBox { property myModule_IntWrapper testInt: highlightedIndex onHighlightedIndexChanged: testInt.intValue = highlightedIndex }...but that's not very elegant.
-
@kkoehne However, does this even work for properties of value types?
I have something like
namespace MyModule { class IntWrapper { Q_GADGET QML_VALUE_TYPE(myModule_IntWrapper) Q_PROPERTY(int intValue READ getIntValue WRITE setIntValue) Q_CLASSINFO("DefaultProperty", "intValue") public: int m_Value = {}; int getValue() const; void setValue(const int& arg); }; } // namespace MyModuleAnd in QML:
import MyModule; ComboBox { readonly property myModule_IntWrapper testInt: highlightedIndex }I would expect testInt to be initialized with the highlightedIndex, but instead I get
Unable to assign int to MyModule::IntWrapper.I can do this:
ComboBox { property myModule_IntWrapper testInt: highlightedIndex onHighlightedIndexChanged: testInt.intValue = highlightedIndex }...but that's not very elegant.
@Asperamanca
So I solved this using an undocumented feature:namespace MyModule { class IntWrapper { Q_GADGET QML_VALUE_TYPE(myModule_IntWrapper) Q_PROPERTY(int intValue READ getIntValue WRITE setIntValue) QML_STRUCTURED_VALUE public: Q_INVOKABLE IntWrapper(const int& value); int m_Value = {}; int getValue() const; void setValue(const int& arg); }; } // namespace MyModuleEDIT: This is going to be documented in Qt 6.7, see https://bugreports.qt.io/browse/QTBUG-113490?jql=text ~ "QML_STRUCTURED_VALUE"