Q_PROPERTY: MEMBER vs READ / WRITE

I read the Qt 5.5 documentation about the Q_PROPERTY macro, but I can't figure it out well enough.

I understand that you can use this macro with the keyword MEMBER or Accessors READ / WRITE. If you use the MEMBER keyword, you do not need to write accessors, because you can access your private data member (property) using setProperty () and Property (), both set and get.

Point: is there a difference between using MEMBER and using READ / WRITE? when should you use one and the other way?

If necessary:

An example of using MEMBER:

Q_PROPERTY(QString text MEMBER m_text NOTIFY textChanged) 

An example of using READ / WRITE:

 Q_PROPERTY(int propX READ getX WRITE setX) 
+6
source share
2 answers

After carefully reading the documentation , it seems to me that there are minor, important differences.

Primarily:

MEMBER variable binding is required if the READ access function is not specified. This makes this variable variable readable and writable without the need to create READ and WRITE access functions.

This means that you can use MEMBER and rely on automatically generated trivial access functions or define these functions for yourself if they turn out to be more complicated than default.

In other words, if your access functions are all the same, for example:

 int propName() const { return prop; } 

So MEMBER excellent. This is not the case if you have something like:

 int propName() const { return superComplexMathUsedToComputeProp(); } 

Also note that:

The READ, WRITE, and RESET functions can be inherited. They can also be virtual.

If you are dealing with a hierarchy, perhaps you want them to be inherited, so perhaps it would be better with READ and WRITE .

What is better and what to use depends on the specific problem.

+7
source

MEMBER only creates the ReadProperty and WriteProperty functions in the qt meta-object system (see the generated moc file). This is useful for interacting with QML. To use a property in C ++, you must also use getters and setters.

So MEMBER -> only for QML READ, WRITE, NOTIFY -> C ++ and QML

If you want to avoid programming trivial getters and setters, define your own packaging makro Q_PROPERTY.

+1
source

All Articles