In the previous article, we introduced the process of declaring the QT attribute, this article mainly introduces the keywords that can be used in Q_property ().
In Qt5.6, Q_property () has a total of 12 keywords, this article describes three: READ, write, and member. Why the first of these three? Because a property must have a read operation, if there is no read definition, you must define member, otherwise this property cannot be accessed, what is the meaning of an inaccessible property? Write is a read-related operation, and it is optional.
Read fun: The interface that defines the read attribute Fun,fun must return the type of the property or attribute the same type reference; fun cannot take parameters.
WRITE fun: Defines an interface that sets Zodiac Fun,fun has no return value, must have a parameter, pass a value or pass a reference, and the parameter type is the same as the zodiac type.
MEMBER var: MEMBER indicates that the member variable VAR is readable and writable, equivalent to using both the read and write keywords. If you do not define read, you must define member, define member, and still use read or write to control the Access interface.
We can think of a different way to understand read, write, and member three keywords: properties are similar to member variables, there are two ways to modify member variables, one is through the interface of the class, and one is accessed directly from the object of the class. Read and write are the first methods that define the interface for read-write properties (member variables), and member This is the second method of directly manipulating member variables.
It is also an example of how to deepen understanding.
The declaration file is as follows:
#ifndef Cobj_h
#define cobj_h
#include <QObject>
class cobj : public qobject
{
Q_object
// READ
q_property(bool p1 READ isintest)
// Read and write
q_property(bool p2 READ isintest WRITE Settest)
// MEMBER
q_property(bool p3 MEMBER m_bflag)
// READ, write, and member
q_property(bool P4 MEMBER m_bflag READ isintest WRITE settest)
// Read and member
q_property(bool P5 MEMBER m_bflag READ isintest)
// Write and member
Q_property (bool P6 MEMBER m_bflag WRITE settest)
Public:
Explicit cobj (qobject *parent = 0 );
Signals:
Public Slots:
Public:
bool isintest (void) const;
void settest (bool bflag);
Private:
bool M_bflag;
};
#endif // cobj_h
The implementation file is as follows
#include "Cobj.h"
cobj:: CObj (qobject *parent) : qobject(parent)
{
}
BOOL cobj:: Isintest (void) Const
{
return (true);
}
void cobj:: Settest (bool bflag)
{
m_bflag = bflag;
}
The CObj class inherits the Qobject, we define 6 properties in CObj, p1 to P6, describe the use combination of read, write, and member, P1 uses the read and write keywords, read,p2 uses the P3 keyword, P4 uses the read, write, and member keywords, P5 uses the Read and member keywords, and P6 uses the write and member keywords.
Read the above several properties read and write methods, summed up:
1 Q_property the attribute defined must have a read method, the property cannot be obtained is meaningless (unreadable property is not a good attribute);
2 definition attribute Read method keywords have two: Read and Member,read define the member function (interface) of the Read property, member define the member variable of the attribute association;
The 3 write defines the writing property interface, which is not required;
4 Q_property macros cannot use commas (,), only spaces.