c++qtqproperty

Can I access a QPROPERTY declared as MEMBER in C++?


I have a QQuickItem class with some members used in QML, so they are declared like this :

Q_PROPERTY (bool myBool MEMBER _myBool NOTIFY myBoolChanged)

If I want to access to this member in C++ code (from another class), do I have a free get-accessor ? What is its syntax ?

The doc is unclear to me :

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.

Does this make the member readable and writable only in QML or also in C++ ?


Solution

  • Qt doesn't generate any C++ API getter for you, but the property value can be read via the meta-object system if you want to go that way:

    QMetaProperty prop = obj->metaObject()->property(...);
    bool value = prop.read(obj).toBool();
    

    Unless you're working on something generic based on the meta-object system, you might want to define a normal getter for use in C++. The MEMBER keyword for Q_PROPERTY is not a shortcut around standard programming practices.