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