The property System
QT provides a sophisticated property system similar to that provided by other compiler vendors. However, as a compiler and platform-Independent library, QT does not rely on nonstandard compiler features, such as __property or [property]. QT Solutions work with any standard C + + compiler on QT-capable platforms. It relies on the meta-object System.
Requirements for Declaringproperties
To declare an attribute, the Q_property () macro is used in the class that inherits Qobject.
Q_property (type name
READ getfunction
[WRITE Setfunction]
[RESET Resetfunction]
[NOTIFY Notifysignal]
[REVISION int]
[designable BOOL]
[Scriptable BOOL]
[STORED BOOL]
[USER BOOL]
[CONSTANT]
[FINAL])
The following is a typical property declaration from the Qwidget class:
Q_property (bool Focus READ Hasfocus)
Q_property (bool enabled READ isenabled WRITE setenabled)
Q_property (qcursor cursor READ cursor WRITE setcursor RESET unsetcursor)
The behavior of a property is the same as a data member, but it has additional features through Meta-object System.
· A read function is required. The value used to read the property. Ideally, it is best to be a const function that must return the type of the property or a pointer or reference to the property type, Qwidget::focus is a read-only property, and the Read function Qwidget::hasfocus ().
· The WRITE function is optional. It is used to set the property value: It must return void and take a parameter with the argument as the type of the property or a reference or pointer to the type. If qwidget::enabled has a WRITE function qwidget::setenabled (): A read-only property does not require a write function, such as Qwidget::focus without the write function.
· RESET The function is also optional. It is used to set the default value that the property value specifies for the context. Qwidget::cursor has typical READ and WRITE functions, Qwidget::cursor () and Qwidget::setcursor (), and it has reset function, Qwidget::unsetcursor (),. Because no call to Qwidget::setcursor () can be set, the context specifies the cursor. The reset function must return void and without parameters.
· The NOTIFY signal is optional. If defined, it is specified as a signal that already exists in the class, and if the value of the property changes, the signal is emitted.
· The number of REVISION is optional. If included, it defines the property and its notification signal, which is used in a specific API version that is exposed to QML.
· The designable property indicates whether the GUI design tool in the property edit is visible. Most properties are designable (default true), instead of true or FALSE, you can specify a Boolean member function.
· The scriptable property indicates whether the script engine can access the property (default true). Instead of true or FALSE, you can specify a Boolean member function.
· The STORED property indicates whether the property should be considered to be self-contained or dependent on other values, and also indicates that the property value must be saved when the state of the stored object is. Most of the properties are STORED (default true), but the STORED of Qwidget::minimumwidth () is false because its value is from the property qwidget::minimumsize () , obtained.
The user property indicates whether the property is recognized as user-oriented or user-available for editing. Typically, there is only one user property for each class, such as for a button qabstractbutton::checked is a user-editable property. Note: Qitemdelegate gets and sets the USER property of the widget.
· The CONSTANT property indicates that the property value is constant. For a given object instance, the READ function returns the same value each time the call is returned. The constant values of different object instances may differ, and the constant property cannot have the WRITE function NOTIFY signal.
The final property indicates that the. property cannot be overridden by a quilt class. In some cases, performance optimizations can be used, but the MOC is not enforced. Caution You cannot override the FINAL property.
READ, WRITE, and RESET functions can be inherited or virtual functions. If the classes are in multiple inheritance, they must be from the first inherited class.
The property type is any type supported by Qvariant, or it can be a user-defined type. In this example, the Qdate class is considered to be a custom type.
Q_property (qdate date READ getDate WRITE setDate)
Because qdate is user-defined, you must include the header file <QDate> when declaring the property.
For Qmap, Qlist, and Qvaluelist properties, the value of the property is a qvariant that contains the entire list or map. Note: The Q_property string cannot contain commas because the comma splits the macro parameters. Therefore, you must use qmap instead of qmap<qstring,qvariant> to do the attribute type. Consistent, replacing qlist<qvariant> and qvaluelist<qvariant> with qlist and qvaluelist ..
Reading and writingproperties with the Meta-object System
You can use the same function Qobject::p roperty () and Qobject::setproperty () to read and write properties. Other information about the class is not known except for the name of the property. The following code snippet, calling Qabstractbutton::setdown () and Qobject::setproperty () are all set to the "Down" property.
Qpushbutton Qpushbutton;
qobject*object = button;
Button->setdown (TRUE);
Object->setproperty ("Down", true);
It is best to access the property with the WRITE function, because . It is faster and better judged at compile time, but setting properties in this way you need to know the classes in the runtime. Accessing properties by property name gives you access to classes that you do not know at run time. You can get the properties of the class at run time by querying its qobject, Qmetaobject, and Qmetaproperties.
qobject*object = ...
qmetaobject *metaobject = Object->metaobject ();
int count = Metaobject->propertycount ();
for (int i=0; i<count; ++i) {
qmetaproperty metaproperty = Metaobject->property (i);
char *name = Metaproperty.name ();
qvariant value = object->property (name);
...
}
In the above code, Qmetaobject::p roperty () is used to get the metadata for each property defined in the location class. Gets the property name from the metadata and passes it to Qobject::p Roperty () to get the value of the property for the current object.
A Simple Example
Suppose we have a class MyClass, which inherits the Qobject and declares the Q_object macro in the private segment. We want to declare a property in MyClass to follow a priority value. The property is named Priority , and its type is the enumeration priority, type defined in MyClass.
We declare this property in the private segment with the Q_property () macro, the READ function is priority, and the WRITE function is setpriority. 。 The enumeration type must be registered with the Q_enums () macro. Registering an enumeration type makes the enumeration type name for Qobject::setproperty (). The call is valid. We must provide our own declaration for the READ and WRITE functions. The MyClass statement may be as follows:
Qobject
{
Q_object
Q_property (Priority priority-READ priority WRITE setpriority NOTIFY prioritychanged)
Q_enums (priority)
Public
MyClass (qobject *parent =0);
~myclass ();
Enum Priority {High, Low, Veryhigh, verylow};
void setpriority (priority priority)
{
M_priority = priority;
Emit prioritychanged (priority);
}
Priority priority () const
{return m_priority;}
Signals:
void prioritychanged (priority);
Private
Priority m_priority;
};
The READ function is const and returns the type of the property, andWRITE returns a void with an argument with the property type. The Meta Object compiler executes these requirements.
Given a pointer to a MyClass object or a qobject pointer to an MyClass instance, there are two ways to set its properties:
MyClass *myinstance =new MyClass;
qobject*object = myinstance;
Myinstance->setpriority (Myclass::veryhigh);
Object->setproperty ("Priority", "Veryhigh");
In the example, the enumeration type defined in MyClass and registered with Q_enums () is the type of the property, which makes the enumeration value a valid string in the SetProperty () call. If an enumeration type is declared in another class, its fully qualified name is required, and the class must inherit Qobject and register the enumeration type with the Q_enums () macro.
A similar macro, Q_flags (),. Like Q_enums (), a macro, it registers an enumeration type, but it has a tag type of a set of tokens, and the value can be manipulated with or. An IO class may have read and Write enumeration values, and Qobject::setproperty () can receive Read | Write: The enumeration value should be registered with Q_flags ().
Dynamic Properties
Qobject::setproperty () can also be used to add new properties to the runtime's class instance. When the function is called with a name and value, if the given Name property exists in Qobject, and the given value is compatible with the type of the property, then value is stored in the property and returns True. If value and the type of the property are incompatible, the value of the property is not changed and returns FALSE. However, if the given Name property does not exist in Qobject, the new property name and value, value, are automatically added to Qobject, but still return false. This means that returning false cannot be used to determine whether a particular property is set, unless you know beforehand that the attribute exists in Qobject.
Note: Thedynamic property is added to the root of each instance, and they are added to Qobject instead of Qmetaobject. An instance property can be dropped by using the Qobject::setproperty () to pass a property name and an illegal qvariant value. The default constructor for Qvariant constructs an illegal value.
As with Q_property () declared properties, the Dynamic property can be queried using Qobject::p roperty ().
Properties and Customtypes
Property types are custom types that need to be registered with the Q_declare_metatype () macro so that the values of the custom type can be stored in a Qvariant object. This makes them suitable for static properties that are declared by Q_property () macros and properties that are created at run time.
Adding Additionalinformation to a Class
Q_classinfo () is an extra macro attached to the property system that can add additional name-value information to the metadata of the class, such as:
Q_classinfo ("Version", "3.0.0")
Like other metadata, class information is also available through the runtime's metadata object.
http://blog.csdn.net/hai200501019/article/details/9204215
QT attribute System