QT attribute System (QT property systems)

Source: Internet
Author: User

Qt Property system QT provides a clever property system similar to the property systems supported by some compilers. However, as a platform-and compiler-agnostic library, QT cannot rely on non-standard compiler features such as __property or [property]. QT's solution can be supported by standard C + + compilers under any QT-supported platform. It relies on the meta-object system (Meta_object Sytstem), and the meta-object system provides a mechanism for communication between objects through signals and slots. How to declare a property by using the Q_property macro in the private domain of the subclass of the Qobject propertyQ_property(Type name (READ GetFunction [WRITE Setfunction] | MEMBER membername [(READ getfunction | WRITE setfunction)]) [RESET Resetfunction] [NOTIFY Notifysignal] [REVISION INT] [designable BOOL] [scritable BOOL] [STORED BOOL] [USER BOOL] [CONSTANT]                        [FINAL]) The following are some of the property declarations from the Qwidget classQ_property(bool Focus READ Hasfocus)Q_property(bool enabled READ isenabled WRITE setenabled)Q_property(qcursor cursor READ cursor WRITE setcursor RESET unsetcursor) The following example shows how to use the Member keyword to export a class data member as a QT attribute. Note that NOTIFY signal must be specified so that it can be qml usedQ_property(Qcolor color MEMBER m_color NOTIFY colorchanged)Q_property(Qreal spaing MEMBER m_spacing NOTIFY spaingchanged)Q_property(QString text MEMBER m_text NOTIFY textChanged) ... signals:void colorchanged (); void spacingchanged (); void TextChanged ( Const QString &nettext);  private:qcolor  m_color;qreal     m_spacing; QString m_text;  A property behaves like a normal data member, but it has additional features that are accessible to the meta-object system. If the member keyword is not specified, a read access function is required. It is used to access the value of the data member. Ideally, it is a constant member function whose return type must be a property type or a constant reference to a property type. For example, Qwidget::focus is a read-only property, accessed through a read function, Qwidget::hadfocus. A write function is optional. It is used to set the value of the data member. Its return type must be void and can have only one parameter whose type must be a property type or a pointer type for the property type, or a reference to a property type. For example, qwidget::enabled has a write function, qwidget::setenabled (BOOL). The Write function is not required for read-only properties. For example, Qwidget::focus has no write function. If the property does not have a read access function, you need to specify the member variable with member. This allows the given member variable to be read-writable without creating read and write functions. If you need to control access to variables, you can also use the read and write functions instead of just member (but don't use them at the same time). A reset function is also optional. It is used to set the property to the default value specified by the context. For example, Qwidget::cursor has the read and write functions, Qwidget::cursor () Qwidget::setcursor (), and it also has a reset function qwidget::unsetcursor (), The cursor property is reset to the default value of the context because there are no available setcursor calls to be determined. The reset function must return a void type with no arguments. The Notify is also optional. If notify is defined, an already existing signal needs to be specified, and the signal will be emitted when the property value is changed. The signal associated with the attribute must have one or 0 parameters, and must be associated with the type of the propertySame. The parameter is the new value of the data member. Notify signals should be emitted only when the value of the property is really changed to avoid being re-evaluated by the QML. Revision is also optional, if the keyword is included, it defines the attribute and the signal is used by a particular version of the API, usually qml. If the keyword is not included, its default is 0. designable Specifies whether the property is visible in the GUI editor (for example, Qtdesigner). Most of the properties are designed (designable defaults to True). In addition to true and false, you can also specify a Boolean member function. The Scritable property specifies whether the property can be accessed by the script engine, which is implicitly true. In addition to true and false you can also specify a Boolean function. The stored property specifies whether the property is independent or dependent on other properties. It also specifies whether the property will be saved when the object property is saved. The stored of most properties is true. However, the stroed of Qwidget::minmunwidth () is false because its value is obtained from Qqwidget::minimumsize () and its type is qsize. User Specifies whether the property is designed to be visible and editable by the user. Under normal circumstances, only one user property is used per object (false by default). For example, qabstractbutton::clicked to buttons is editable (checkable). Note, Qitemdelegate sets the user property of the widget using the set and Access function color. The appearance of the constant indicates that the property is a constant value. For a given point object instance, every call to the Read function should return the same value. This property may not be the same for different instances. There can be no write function and notify signal at the same time. Final indicates that the property is no longer overridden in subclasses. In some cases it is used to optimize performance, but it is not implemented by MOC. It is important to note that the Fianl property is never overwritten in subclasses. The READ WRITE reset function can be inherited. They can also be virtual functions. When used in a class that uses multiple inheritance, it must come from the first class. The property type can be any qvariant supported property, or a user-defined property. In this example, the class qdate is considered a user-defined type.  q_property (qdate data READ getDate WRITE setDate)   Because qdate is user-defined, when declaring a property, you must include the <QDate> header file. Due to historical reasons, Qmap and QLIST is synonymous with qvariantmap and qvariantlist.   Read and write properties using meta-object systems A property can be accessed and set by Qobject::p operty () function, Qobject::setproperty () function. You do not need to know the name of the class other than the property's. In the following code snippet, the calling function Qabstractbutton::setdown () and the function Qobject::setproperty () are set to the property "down" qpushbutton* button = new Qpushbutton; qobject* object = Button;button->setdown (true); Object->setproperty ("Down", true); Setting property values through the Write function is better than both. Because it is more efficient and has a better diagnosis at compile time. But this requires you to actually understand the entire class in the compilation (which can access its definition). Accessing a property by property name allows you to access or set properties without knowing the definition of the class. You can query the class properties at run time through Qobject,qmetaobject and qmetaproperties.  qobject *object = ... const qmetaobject *metaobject = Object->metaobject (); int count = metaobject-> Propertycount (); for (int i = 0; i< count; ++i) {    Qmetaproperty metaproperty = Metaobject->property (i);     cont char *name = metaproperty.name ();    qvariant value = object->property (name);}   in the code snippet above, Qmetaobject::p roperty () is used to get metadata defined in an unknown class. The name of the property is obtained by metadata and passed to Qobject::p Roperty () to get the property value.   Suppose we have a simple class MyClass that inherits from Qobject and uses Q_OBJEC in the private domainT. We want to declare a property to track permission values. The name of the property is priority, and its type is the priority enumeration that is defined in MyClass. We use Q_property to declare attributes in the private zone. The read function is the priority (), and the Write function is SetPriority (). The enumeration type needs to be registered with the Q_enum () macro in the Meta-object system. Registering an enumeration type allows the enumeration to be used in the Setpropert function. We must also provide a declaration of the read and write functions. The class is defined as follows: Class Myclass:public qobjct{    q_object    Q_property (Priority priority-READ priority WRITE SetPriority NOTIFY prioritychanged) public:    Explicit MyClass (Qobject *parent = 0);    ~myclass (); nbsp;    Enum Priority {High, Low, Veryhigh, Verylow};    q_enum (priority)      Voi D SetPriority (priority priority)     {        m_priority = priority;   } &nbs P   Priority () const {return m_priority;}  signals:    void prioritychanged (priority); private:    priority m_priority;}; The  read function is a regular member function and returns the priority type. The Write function returns void and has only one parameter of type priority. Given a pointer to a MyClass instance of type MyClass or Qobject, we have two ways to set itsThe priority attribute. MyClass *myinstance = new MyClass; Qobject *object = myinstance; myinstance->setpriority (Myclass::veryhigh); Object->setproperty ("Priority "," Veryhigh "); In this example, the enumeration type defined in MyClass is the type of the property and is registered in the Meta-object system by the Q_enum () macro. This allows the enumeration type to be accessed through a string in SetProperty (string), using an enumeration type defined in another class, which must be fully declared (i.e. Otherclass::P riority). And that class should inherit from Qobject and be registered with the Q_enum () macro. A similar macro Q_flag (). Like Q_enum (), it registers an enumeration type, but marks it as a series of flags, which can be used or manipulated. An IO class has a read and write enumeration value, and can then pass in Qobject::setproperty to read | Write access. Q_flag () should be used to register enumeration types.   Dynamic Property Qobject::setproperty () can also be used to add properties to a class instance at run time. When the function is called when the name and value are passed in. If the property name already exists in the class and the incoming type is compatible with the type of the property, the property value is saved and returned true, otherwise the value is not modified, but the function returns false. However, if the given property name does not exist, the new attribute is added to the class when the function still returns false. This means that the return value of the function cannot be used to determine whether the property value is successfully set. Unless you already know if a property exists before. Note: Dynamic properties are added to each instance. That is, they are added to Qobject instead of Qmetaobject. You can remove an attribute by passing an empty qvariant to the SetProperty function. The default constructor for Qvariant constructs an invalid Qvariant object. Dynamic properties can be queried by Qobject::p Roperty (), just like the properties defined by Q_property. The custom types that are used by properties and custom types are required to be registered with the Q_declare_metatype macro. This allows the Qvariant object to save the value of the class. This is true for both dynamic and static properties. Adding additional information to the class corresponds to the property system is the Q_classinfo(name, value) macro. This macro adds name-value to the class's meta-object. For example: Q_classinfo ("Version", "3.0.0") and the meta-object data, the class information can be accessed at run time through the Qmetaobject::classinfo function.  by LINANNK&NBSP;2016/6/2 1:33  attached: the so-called add attribute to Qoject two is not qmetaobject meaning: Assume: There are two instances of the MyClass object A and B, when a dynamic add a property, B is not affected. The Qmetaobject is shared by all MyClass instances.   for Q_declare_metatype, another important use is for registering signals and user-defined types used in slots. If the signal and slot are connected using qt::queuedconnection, you also need to register with the qregistermetatype<t> () function.   Also, QT's state machine framework and animation framework depend on the property system. This document translates most of the QT documentation, plus some of the experience of using QT.

Qt Property System (QT)

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.