简体   繁体   English

Q_PROPERTY:MEMBER vs READ / WRITE

[英]Q_PROPERTY: MEMBER vs READ/WRITE

I was reading the documentation of Qt 5.5 about Q_PROPERTY macro, but I can't understand it well enought. 我正在阅读关于Q_PROPERTY宏的Qt 5.5的文档,但我无法理解它。

I understand that you can use in this macro with the keyword MEMBER or the accessors READ/WRITE instead. 我知道您可以在此宏中使用关键字MEMBER或访问者READ / WRITE代替。 If you use keyword MEMBER you don't have to write the accessors, because you can access to your private data member (the property) with the use of setProperty() and Property(), like a set and get. 如果使用关键字MEMBER,则不必编写访问者,因为您可以使用setProperty()和Property()访问您的私有数据成员(属性),如set和get。

The point is: is there any difference between using MEMBER and using READ/WRITE? 关键是:使用MEMBER和使用READ / WRITE之间有什么区别吗? when should you use one and when the other way? 什么时候应该使用一个而另一个方式?

For if necessary: 如有必要:

Example of using MEMBER: 使用MEMBER的示例:

Q_PROPERTY(QString text MEMBER m_text NOTIFY textChanged)

Example of using READ/WRITE: 使用READ / WRITE的示例:

Q_PROPERTY(int propX READ getX WRITE setX)

By reading carefully the documentation , it seems to me that there are slightly, important differences. 通过仔细阅读文档 ,在我看来,存在轻微,重要的差异。

First of all: 首先:

A MEMBER variable association is required if no READ accessor function is specified. 如果未指定READ访问器函数,则需要MEMBER变量关联。 This makes the given member variable readable and writable without the need of creating READ and WRITE accessor functions. 这使得给定成员变量可读写,而无需创建READ和WRITE访问器函数。

That means that you can either use MEMBER and rely on auto generated, trivial accessor functions or define for yourself those functions if they happen to be more complex than a defaulted one. 这意味着您可以使用MEMBER并依赖自动生成的,简单的访问器函数,或者如果它们恰好比默认函数更复杂,则自己定义这些函数。

In other terms, if your accessor functions are all the way the same, as an example: 换句话说,如果您的访问器功能完全相同,例如:

int propName() const { return prop; }

Thus, MEMBER is fine. 因此, MEMBER很好。 It does not if you have something like: 如果您有以下内容,则不会:

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

Also, note that: 另请注意:

The READ, WRITE, and RESET functions can be inherited. READ,WRITE和RESET函数可以继承。 They can also be virtual. 它们也可以是虚拟的。

If you are dealing with a hierarchy, maybe you want them to be inherited, so maybe to go with READ and WRITE would be better. 如果你正在处理一个层次结构,也许你希望它们被继承,那么也许选择READWRITE会更好。

Which is the best and what to use depends on the specific problem. 哪个是最好的,使用什么取决于具体问题。

The MEMBER creates just ReadProperty and WriteProperty features in qt meta object system(see the generated moc file). MEMBER在qt元对象系统中仅创建ReadProperty和WriteProperty功能(请参阅生成的moc文件)。 This is useful for interfacing with QMLs. 这对于与QML的接口很有用。 In order to use property in c++, the getters and setters has to be implemented as well. 为了在c ++中使用属性,还必须实现getter和setter。

So MEMBER -> just for QMLs READ, WRITE, NOTIFY -> C++ and QML 所以成员 - >仅适用于QMLs READ,WRITE,NOTIFY - > C ++和QML

If you would like to avoid programming trivial getters and setters, define your own makro wrapping Q_PROPERTY. 如果您想避免编写琐碎的getter和setter,请定义自己的makro包装Q_PROPERTY。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM