Property settings in Qt

1. Properties in Qt

Attributes refer to the attributes of windows or controls. For example, the opacity attribute represents "transparency", geometry refers to "position and size", and pos attribute represents "position". The controls in qt have their own properties, and we can also define properties ourselves.

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

In addition to the above methods, there is another way to customize properties, which is to use the Q_PROPERTY macro. The simple usage is as follows:

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 property type is bool type, and bIsDoubi is the property name. In addition, two functions need to be written, the first is the function void setDoui(bool) for setting attributes, and the second is the function bool getDoubi() for obtaining attributes.

Second, what is the use of custom attributes

The custom attributes that I know so far have two uses, the first is for changing styles, and the second is for animation, which are explained below.

3. Change the style

Open the Qt Assistant, find the syntax part of the style sheet, there is an attribute selector in setting the style through the selector, for example, QPushButton[flat="false"] means the style when the button attribute flat is false.

For example, we have a class named PropertyTest, and a button in the interface named pushButton_3

1

2

3

4

#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 default style of the button is blue blue, and the color of the button can be changed by changing the property borderColor value of the class PropertyTest.

In code, first define the property

1

Q_PROPERTY(QString borderColor READ getBorderColor WRITE setBorderColor)

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

1

2

3

4

5

private:

QString m_strBorderColor;

private:

void setBorderColor(const QString &strBorderColor){ m_strBorderColor = strBorderColor; }

QString getBorderColor(){ return m_strBorderColor; }

Click the button pushButton to change the attribute value, thereby changing the style of the button pushButton_3.

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

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++;

}

最后要注意的是,上面代码中的unpolish和polish部分。

在助手中有个提醒,如果使用属性选择器的时候,之前已经有样式,那么有必要重新设置一下,就是先去掉之前的样式,再添加新的样式。也就是通过上面unpolish和polish两个函数实现。

四、动画中使用自定义属性

如果我们想要用一个动画改变按钮的透明度,因为按钮QPushButton是继承自QWidget的,在QWidget中有个函数setWindowOpacity,所以你会将动画的属性名称设置为windowOpacity。然而,最后透明度是不会有任何改变的,因为只有在setWindowFlags时设置为Qt::Window,windowOpacity这个属性才能生效。

因此,有必要寻求其它方法,在QWidget中有一个函数setGraphicsEffect(QGraphicsEffect *),其中QGraphicsEffect有一个派生类QGraphicsOpacityEffect,可以通过它来设置QWidget的透明度。

1

2

3

m_pOpacityEffect = new QGraphicsOpacityEffect(this);

m_pOpacityEffect->setOpacity(1);

this->setGraphicsEffect(m_pOpacityEffect);

1

Q_PROPERTY(qreal buttonOpacity READ buttonOpacity WRITE setBtnOpacity)

定义属性时,在函数setBtnOpacity中改变QGraphicsOpacityEffect对象,来调整透明度。

好了,现在我们将动画属性名称设置为buttonOpacity,就能改变按钮的透明度了

本文福利,费领取Qt开发学习资料包、技术视频,内容包括(C++语言基础,C++设计模式,Qt编程入门,QT信号与槽机制,QT界面开发-图像绘制,QT网络,QT数据库编程,QT项目实战,QSS,OpenCV,Quick模块,面试题等等)↓↓↓↓↓↓见下面↓↓文章底部点击费领取↓↓

Guess you like

Origin blog.csdn.net/m0_73443478/article/details/130581533