How to redefine an existing Q_PROPERTY as REQUIRED?

scopchanov

Goal

I have a QQuickItem derived C++ class, Foo, and I would like to ensure, that whoever uses it in QML, gives a value to the objectName property. In Foo I am redefining the property like this:

Q_PROPERTY(QString objectName READ objectName WRITE setObjectName NOTIFY objectNameChanged REQUIRED)

Since objectNameChanged does not take zero arguments, this alone leads to an error. So I need to also add a signal to Foo:

void objectNameChanged();

Then it compiles without errors.

Problem

The documetation states:

In QML, classes with REQUIRED properties cannot be instantiated unless all REQUIRED properties have been set.

However, when I use it like this in QML:

Foo {
    // objectName: qsTr("test) <-- commented line
}

the program compiles and runs without errors.

Question

Is there a way to change the objectName, or any other existing property for that matter, to required?

Inkane

What you could do is to expose a qml file Foo.qml which contains

CppFoo {
    required objectName
}

instead of exposing the C++ class directly to your users. Doing that for instance with a simple Item, and using it in another file would then lead to an error like

file:///tmp/tmp.aOQi2yetXl/Foo.qml:3 Required property objectName was not initialized

The fact that Q_PROPERTY(QString objectName READ objectName WRITE setObjectName NOTIFY objectNameChanged REQUIRED) did not work is probably a bug in the engine. And there's currently no feature in the C++ API to mark the property of a parent's class as required.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How to override a Q_Property?

From Dev

Using an existing Q_PROPERTY to animate QGraphicsItem inheriting QObject

From Dev

Redefine an existing function

From Dev

Redefine an existing function

From Dev

How does Q_PROPERTY() RESET clause work?

From Dev

How does Q_PROPERTY() RESET clause work?

From Dev

How to change getter signature generated by Qt Creator for Q_PROPERTY?

From Dev

How do I redefine a property from a prototype?

From Dev

Redefine Macro in existing C library

From Dev

How to expose a pointer to a Q_GADGET to QML through a Q_PROPERTY

From Dev

C++ redefine output stream of existing class

From Dev

Doxygen: How to redefine void

From Dev

How redefine a loaded class?

From Dev

How to redefine a key in SBT?

From Dev

Rails: How to redefine a Class

From Dev

How to redefine an array in one statement?

From Dev

How to redefine a LUA function globally

From Dev

How to redefine \s to match underscores?

From Dev

How to redefine private method in javascript?

From Dev

How to change/redefine function by itself

From Dev

How to redefine an array in one statement?

From Dev

How to redefine timepicker input field

From Dev

How to redefine a LUA function globally

From Dev

How write macro to avoid redefine?

From Dev

Vue js: cannot redefine property: $url

From Dev

How to add required to model property in controller mvc?

From Dev

What is the significance of Q_PROPERTY in Qt?

From Dev

Expose abstract type as Q_PROPERTY to QML

From Dev

Q_PROPERTY with parameterized read and write accessors