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 using QMetaProperty::isWritable() method to enable or disable a widget that will be used to edit the corresponding property.
    I'm also using MEMBER feature to keep my member variables with a m_ 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.



  • I just realized that I can declare a property like this:

    Q_PROPERTY(QString name READ name)

    and I do not necessarily need to have a member in my QObject to store the property.

    But I still don't know the implications of doing so.


  • Qt Champions 2016

    @mcleary
    What about:

    Q_PROPERTY(QString name MEMBER m_name READ name)
    

    This should generate your variable and a getter, but without the setter.



  • @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:
    Where m is some QObject with Q_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 the QMetaProperty is reporting that the property is writable.
    I solve my problem removing the MEMBER declaration!


  • Qt Champions 2016

    @mcleary

    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,


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.