qt--Custom Properties

Source: Internet
Author: User

First, the properties in QT

A property is a property of a window or control, such as a opacity property that represents "transparency", geometry refers to "position and size," and the Pos property represents "location." The controls in Qt have their own properties, and we can define them ourselves.

Qobject This class has a function setProperty, we can define our own properties through this function, the use of the method is very simple, setProperty (const char * name, const qvariant & value), The first parameter is the name of the property, and the second parameter is the property value.

In addition to the method above, there is a way to customize the properties, that is, using the Q_property macro, the following simple usage:

Q_property (type name READ getfunction WRITE setfunction)

Q_property (parameter type parameter name READ Get property value function WRITE Set property value function)

For example Q_property (bool Bisdoubi READ Getdoubi WRITE setdoubi), the attribute type is bool type, and Bisdoubi is the property name. In addition to writing two functions, the first is to set the property of the function void Setdoui (BOOL), the second is to get the property of the function bool Getdoubi ().

Ii. What is the use of custom attributes

I currently know the custom attribute has two uses, the first is to change the style, the second is for the animation, the following is explained separately.

Third, change the style

Turn on the QT helper and find the Stylesheet Syntax section, with a property selector in the selector settings style, such as qpushbutton[flat= "false", meaning the style when the Button property flat to False.

A chestnut, we have a class, called PropertyTest, there is a button in the interface, named Pushbutton_3

#pushButton_3 {border:4px solid blue;} Propertytest[bordercolor= "Red"] #pushButton_3 {border:4px solid red;} Propertytest[bordercolor= "Green"] #pushButton_3 {border:4px solid green;} Propertytest[bordercolor= "Blue"] #pushButton_3 {border:4px solid blue;}

The above style means that the button default style is blue blue, changing the color of the button by changing the bordercolor value of the Class PropertyTest property.

In code, you first define the properties

Q_property (QString bordercolor READ getbordercolor WRITE setbordercolor)

Use a member variable to hold the value of the property, and set and get the value separately through the set and get functions.

Private:    QString m_strbordercolor;private:void setbordercolor (const QString &strbordercolor) {m_ Strbordercolor = Strbordercolor; }qstring Getbordercolor () {return m_strbordercolor;}

Click the button pushbutton change the property value to change the style of the button pushbutton_3.

void Propertytest::changebordercolor () {if (m_itest% 3 = = 0) {setBorderColor ("red");} else if (m_itest% 3 = = 1) {setBorderColor ("green");} Else{setbordercolor ("Blue");} Style ()->unpolish (ui.pushbutton_3); style ()->polish (ui.pushbutton_3); update (); m_itest++;}

Finally, note the Unpolish and Polish sections of the above code.

In the assistant, there is a reminder, if you use the property selector, there is already a style, then it is necessary to reset, is to first remove the previous style, and then add a new style. This is achieved by the above Unpolish and Polish two functions. Iv. using custom attributes in animations

If we want to change the transparency of a button with an animation,

qt--Custom Properties

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.