It is possible to have a read only Q_PROPERTY with MEMBER?
-
I build a system that automatically create interface elements for editing properties declared with Q_PROPERTY.
I'm usingQMetaProperty::isWritable()
method to enable or disable a widget that will be used to edit the corresponding property.
I'm also usingMEMBER
feature to keep my member variables with am_
prefix.
The problem is that, as stated in the documentation:A MEMBER variable association is required if no READ accessor function is specified. This makes the given member variable readable and writable without the need of creating READ and WRITE accessor functions. It's still possible to use READ or WRITE accessor functions in addition to MEMBER variable association (but not both), if you need to control the variable access.
apparently there is no way to create a read-only property using the
MEMBER
feature. So I would like to confirm if I can have a property with this characteristic. -
@kshegunov the problem I was facing is that the
QMetaProperty
was reporting that the property is writable even if I don't specify a setter function.If you want to check, you can use this code:
Wherem
is some QObject withQ_PROPERTIES
auto object = &m; const QMetaObject *metaobject = object->metaObject(); int count = metaobject->propertyCount(); for (int i=0; i<count; ++i) { QMetaProperty metaproperty = metaobject->property(i); const char *name = metaproperty.name(); QVariant value = object->property(name); qDebug() << name << value << "NOTIFY" << metaproperty.hasNotifySignal() << "WRITABLE" << metaproperty.isWritable(); if(!metaproperty.isWritable()) { qDebug() << name << "READONLY" << value; } }
When one declares a property with
MEMBER
feature theQMetaProperty
is reporting that the property is writable.
I solve my problem removing theMEMBER
declaration! -
When one declares a property with MEMBER feature the QMetaProperty is reporting that the property is writable.
This is quite strange indeed.
I solve my problem removing the MEMBER declaration!
This is certainly a possibility. Still I'm glad it's working for you,