Details
-
Suggestion
-
Resolution: Duplicate
-
Not Evaluated
-
None
-
None
-
None
Description
Note: I was not sure which Component this feature request should be filed under, so please change to the appropriate Component.
The REQUIRED keyword can be used to enforce objects instantiated by QML to have a specific property set. This is specially useful to replace values that would be passed to the object constructor in a typical C++ program. Furthermore, constructor arguments are typically passed for object construction and not reset again through object lifetime. To emulate the same behavior, currently one could check in the setProperty method if the component has already been initialized, and if so, not reset the property value:
void componentComplete() // from QQmlParserStatus { // initialize resources using properties m_initialized = true; } void setSomeProperty(Type value) { if (!m_initialized) { // Log error and return } else { m_someProperty = value; } }
However doing this check in every class is cumbersome and cluttering. Considering the commonality of passing constructor arguments as properties to QObject classes, and having initialize-able but not reset-able variables constructor arguments in C++, it would be great to add a Q_PROPERTY attribute, which makes the QML engine be able to instantiate the object with that property but not reset it:
MyComp { id: myComp myProp: value // valid } // cannot reset onSignal: { myComp.myProp = newValue // invalid }
Attachments
Issue Links
- relates to
-
QTBUG-85763 Feature Request: Option to have required and readonly QML property
- Reported