C + + QT Attribute System detailed introduction to _c language

Source: Internet
Author: User
Tags constant

C + + QT Attribute System detailed introduction

QT provides a wonderful attribute system. Similar to those provided by the compiler. However, as a library that is independent of compilers and platforms, QT does not rely on nonstandard compilation features, such as __property or [property]. QT can be compiled on any platform under the standard compiler. The QT attribute system is based on a metadata object system-that is, the guy who provides the object's built-in signal and slot communication mechanism.

What is required to declare a property

To declare a property, you use the Q_property () macro in a class that inherits from Qobject.

Q_property (type name
  READ getfunction
  [WRITE setfunction]
  [RESET resetfunction]
  [NOTIFY Notifysignal]
  [designable bool]
  [scriptable bool] [
  STORED bool]
  [USER bool]
  [CONSTANT]
  [FINAL])


Here are some examples of typical declarative properties:

Q_property (bool Hasfocus) 
Q_property (BOOL-enabled READ isenabled WRITE setenabled) 
Q_property ( Qcursor cursor READ cursor WRITE setcursor RESET unsetcursor) 
  • An attribute behaves like a data member of a class, but it also has additional attributes that can be manipulated by the metadata object system. These features are: a read accessor function is required. The value used to read the property. Ideally, there is an immutable function for this purpose, and it must return a value or a pointer or a reference to the type of the property. For example, Qwidget::focus is a read-only property that corresponds to a read function: Qwidget::hasfocus ().
  • An optional write accessor function. It is used to set the value of the property. It must return empty and have at least one argument, which is the value or pointer or reference of the property type. For example: qwidget::enabled has the Write function qwidget::setenable (). Read-only properties do not require write functions. For example, Qwidget::focus does not have a corresponding write function.
  • An optional reset function. Used to set the value of the property to its default value. For example: Qwidget::cursor has a typical read and write function, Qwidget::cursor () and Qwidget::setcursor (), and it also has a reset function, Qwidget:: Unsetcursor (). The reset function must return void without any arguments.
  • An optional notify signal. If it is defined, the signal is emitted when the value of the property changes. The signal must have a parameter that has the same type as the property, and the parameter holds the new value of the property.
  • A designable variable indicates whether this property appears in the property editor of the interface designer. Most properties are visible, except that you can specify a bool-type member function in addition to TRUE or FALSE for this variable.
  • The scriptable variable indicates whether this property can be manipulated by a script engine (the default is true). You can also give it a true or false or bool type function.
  • The stored variable indicates whether the attribute is considered to be independent or dependent on other values. It also indicates whether the value of this property is saved when the object state is saved. Most properties are required to be saved, but such as qwidget::minimumwidth () is not saved because its value is derived from another property qwidget::minimumsize ().
  • The user variable indicates whether the property is designed to be user-oriented or user-modifiable class properties. Typically, each class has only one user attribute. For example, qabstractbutton::checked is a user-modifiable property of a button class. Note Qitemdelegate gets and sets the user property of the widget.
  • The appearance of the constant indicates that the value of the property is unchanged. For an object instance, the Read method of a constant property must return the same value each time it is invoked. This constant value may not be the same in different instances of object. A constant property cannot have a write method or a noyify signal.
  • The appearance of the final variable indicates that the property cannot be overridden by a derived class. In some cases, this can be used for efficiency optimization, but is not enforced by MOC. Programmers must always be careful not to rewrite a final attribute.

Both the read,write and reset functions can be inherited. They can also be virtual functions. When they are inherited in multiple inheritance, they must appear in the first class that is inherited.

The type of the property can be all types supported by qvariant, or it can be a user-defined type. In the following example, the class qdate is treated as a user-defined type.
Q_property (qdate data READ getDate WRITE setdate)

Since qdate is user-defined, you must include a <QDate> header file.

For the 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 commas divide the parameters of macros. Therefore, you must use Qmap as the type of the property rather than the qmap<qstring,qvariant>. In order to maintain consistency, it is also necessary to use qlist and qvaluelist rather than qlist<qvariant> and qvaluelist<qvariant>.

Read and write properties through the metadata object system

A property can be read and written using a regular function Qobject::p roperty () and Qobject::setproperty () without knowing any details of the class in which the attribute resides, except for the name of the property. In the small snippet below, the call to Qabstractbutton::setdown () and Qobject::setproperty () sets the property to "down".

Qpushbutton *button = new Qpushbutton; 
QObject *object = button; 
Button->setdown (true); 
Object->setproperty ("Down", true); 

Manipulating a property through the write operator is better than both, because it is fast and gives better diagnostic help at compile time, but setting the attribute in this way requires that you understand its class at compile time. Manipulating attributes by name allows you to manipulate classes that you do not understand in the compiler. You can discover the properties of a class at run time by querying its qobject,qmetaobject and qmetaproerties.

QObject *object = ... 
Const Qmetaobject *metaobject = Object->metaobject (); 
int count = Metaobject->propertycount (); 
for (int i=0; i<count; ++i) { 
  Qmetaproperty metaproperty = Metaobject->property (i); 
  const char *name = Metaproperty.name (); 
  Qvariant value = object->property (name); 
  ... 
} 

In the code snippet above, Qmetaobject::p roperty () is used to get the metadata of the properties in the unknown class. Get the property name from the metadata and pass it to Qobject::p Roperty () to get

A simple example

Let's say we have a class MyClass, which derives from Qobject and uses Q_object macros in its private area. We want to declare a property in the MyClass class to keep track of a priorty value. The value of the property is called priority, and its type is an enumeration defined in class MyClass called priority.

We use Q_property () to declare the property in the private area of the class. The read function is called priority, and we include a write function called SetPriority. Enumeration types must be registered with Q_enums () in the metadata object system. Registering an enumeration type allows the name of the enumeration to be used when calling Qobject::setproperty (). We must also provide our own declarations for the read and write functions. MyClass's statement should look like this:

Class Myclass:public QObject 
{ 
  q_object 
  q_property (Priority Priority READ Priority WRITE setpriority) 
  q_enums (Priority) public 
: 
  MyClass (QObject *parent = 0); 
  ~myclass (); 
  Enum Priority {High, Low, Veryhigh, verylow}; 
  void SetPriority (Priority Priority); 
  Priority Priority () const; 
 

The read function is const and returns the type of the property. The Write function returns a parameter of void and has an attribute type. The metadata object compiler enforces these things.

When you have a pointer to a MyClass instance, there are two ways to set the Priority property:

MyClass *myinstance = new MyClass; 
 QObject *object = myinstance; 
 Myinstance->setpriority (Myclass::veryhigh); 
 Object->setproperty ("Priority", "Veryhigh"); 

In this example, the enumeration type is declared in MyClass and is registered with Q_enums () in the metadata object system. This allows the enumeration value to be used as a string when calling SetProperty (). If the enumeration type is declared in another class, then we need to use the full name of the enumeration (such as Otherclass::P riority), and the other class must also derive from the Qobject and also register the enumeration type.

Another simple Q_flags () is also available. Like Q_enums (), it registers an enumeration type, but it takes an enumeration type as a flag collection, that is, the value can be merged with OR. An I/O class may have enumerated values Read and write and Qobject::setproperty () can accept read| Write. You should use Q_flags () to register the enumeration values at this time.

Dynamic Properties

Qobject::setproperty () can also be used to add new properties to an instance of a class at run time. When invoked with a name and value, if a corresponding property already exists, and if the type of the value is compatible with the type of the property, then the value is stored in the property and returns True. If the value type is incompatible, the value of the property is not changed, and false is returned. However, if the attribute of the corresponding name does not exist, then a new attribute is born, with the name passed in as the value of the passed-in value, but still returns false. This means that the return value cannot be used to determine whether a property is set or not, unless you already know that the attribute already exists in the qobject.

Note that dynamic properties are added to the foundation of a single implementation, that is, added to Qobject rather than qmetaobject. A property can be removed from an instance by passing in the name of the property and the illegal qvariant value to Qobject::setproperty (). The default Qvariant constructor constructs an illegal qvariant.
Dynamic properties can be queried using the Qobject::p Roperty (), as in the case of the Q_property () declared by the row.

Properties and Custom Types

Custom types used by properties need to be registered with the Q_declare_metatype () macro so that their values can be saved in the Qvariant object. This allows them to be used in static types declared by Q_property () or in dynamic types.

Thank you for reading, I hope to help you, thank you for your support for this site!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.