当前位置:网站首页>QT笔记——Qt动态属性 之 unpolish() 和 polish()
QT笔记——Qt动态属性 之 unpolish() 和 polish()
2022-07-22 08:12:00 【旧街凉风°】
动态改变样式 即动态属性,如点击按钮可以让按钮的样式改变:从红色->蓝色->绿色
我们来了解一下这个函数:
bool QObject::setProperty(const char *name, const QVariant &value)
这个函数是干什么?
对于一个基于QObject的控件来讲,可以通过setProperty来设置属性
它的参数是什么?
const char *name:即自定义的属性名称,注意不要和控件的默认属性名称相同
const QVariant &value: 为此属性的值
具体的看此链接:QT property属性的应用
它对本文有来说怎么使用呢?
我们的需求:动态改变按钮的样式 ,接下来就会有很大的作用
1.我们需要新建一个类,在ui里放一个pushButton,然后给按钮设置好样式
右键我们的pushButton点击样式表,内容为:
第一行为初始颜色,PropertyTest为我们新建的类名,borderColor为我们需要设置的属性,“=”后的为属性的值,btn为我们pushButton的ObjectName
#btn{
border:4px solid blue;}
PropertyTest[borderColor="red"] #btn{
border:4px solid red;}
PropertyTest[borderColor="green"] #btn{
border:4px solid green;}
PropertyTest[borderColor="blue"] #btn{
border:4px solid blue;}
2,声明点击按钮的槽函数
private slots:
void changeBorderColor(); //改变属性的颜色
private:
int m_iTest; //点击的次数
3.连接我们的槽函数
connect(ui.btn, &QPushButton::clicked, this, &PropertyTest::changeBorderColor);
4.定义我们的槽函数内容
void PropertyTest::changeBorderColor()
{
if (m_iTest % 3 == 0)
{
setProperty("borderColor", "red");
}
else if (m_iTest % 3 == 1)
{
setProperty("borderColor", "green");
}
else
{
setProperty("borderColor", "blue");
}
//手动更新样式的两种方式 第一种
//ui.btn->style()->unpolish(ui.btn); //清理之前的样式
//ui.btn->style()->polish(ui.btn); //用于添加新的样式
//ui.btn->update();
//第二种
ui.btn->setStyle(QApplication::style());
m_iTest++;
}
效果:
第二种方法:
1.同方法一的第一步
2.我们需要添加继承自QObject的类中使用 Q_PROPERTY 宏指令
这个宏是干啥的呢?
看下本例子中:
Q_PROPERTY(QString borderColor READ getBorderColor WRITE setBorderColor)
依据Q_PROPERTY
(参数类型 参数名称 READ 获得属性值函数 WRITE 设置属性值函数)
参数类型:QString
参数名称:borderColor
获得属性值函数:getBorderColor
设置属性值函数:setBorderColor
看具体宏链接:Q_PROPERTY介绍
3.定义我们在宏中写的读写的成员函数 和 保存设置的颜色
public:
void setBorderColor(const QString& strBorderColor) {
m_strBorderColor = strBorderColor; }
QString getBorderColor() {
return m_strBorderColor; }
private:
QString m_strBorderColor;
int m_iTest;
4.同上一个方法的第三步
5.定义我们的槽函数
void PropertyTest::changeBorderColor()
{
if (m_iTest % 3 == 0)
{
setBorderColor("red");
}
else if (m_iTest % 3 == 1)
{
setBorderColor("green");
}
else
{
setBorderColor("blue");
}
//手动更新样式
//ui.btn->style()->unpolish(ui.btn);
//ui.btn->style()->polish(ui.btn);
//ui.btn->update();
ui.btn->setStyle(QApplication::style());
m_iTest++;
}
效果和第一种一样
完整代码:
PropertyTest.h
#pragma once
#include <QtWidgets/QWidget>
#include "ui_PropertyTest.h"
#include <QStyle>
#include <QDebug>
class PropertyTest : public QWidget
{
Q_OBJECT
//Q_PROPERTY(QString borderColor READ getBorderColor WRITE setBorderColor)
public:
PropertyTest(QWidget *parent = Q_NULLPTR);
~PropertyTest();
//void setBorderColor(const QString& strBorderColor) { m_strBorderColor = strBorderColor; }
//QString getBorderColor() { return m_strBorderColor; }
private slots:
void changeBorderColor();
private:
Ui::PropertyTestClass ui;
QString m_strBorderColor;
int m_iTest;
};
PropertyTest.cpp
#include "PropertyTest.h"
PropertyTest::PropertyTest(QWidget *parent)
: QWidget(parent), m_iTest(0)
{
ui.setupUi(this);
connect(ui.btn, &QPushButton::clicked, this, &PropertyTest::changeBorderColor);
}
PropertyTest::~PropertyTest()
{
}
void PropertyTest::changeBorderColor()
{
if (m_iTest % 3 == 0)
{
setProperty("borderColor", "red");
//setBorderColor("red");
}
else if (m_iTest % 3 == 1)
{
setProperty("borderColor", "green");
//setBorderColor("green");
}
else
{
setProperty("borderColor", "blue");
//setBorderColor("blue");
}
//手动更新样式
//ui.btn->style()->unpolish(ui.btn);
//ui.btn->style()->polish(ui.btn);
//ui.btn->update();
ui.btn->setStyle(QApplication::style());
m_iTest++;
}
边栏推荐
- 力扣练习——35 组合总和 II
- Rk3399 platform development series explanation (input subsystem) 4.52. Implementation principle of input subsystem
- Learn TB writing method with SPI simulation file
- mysql存储过程返回的结果集的问题
- Physical layer of network
- 【Excle】生成guid和datetime导入测试数据到数据库
- Oracle statement adjustment
- How to carry out efficient data governance? Index management and data traceability help!
- 力扣练习——24 去除重复字母
- Hblock盘活企业级存储市场
猜你喜欢
Aruba学习笔记04-Web UI --Configuration面板介绍
ieee下载文献的方法
Bigder:37/100 a misoperation
Dokcer running Nacos container automatic exit problem
VCs and Verdi learning records
【微服务~远程调用】整合RestTemplate、WebClient、Feign
web3分享
逆袭黑马:数据库全栈工程师(DevDBOps)培训,把最好的课程送给您!
[case sharing] configure the routing penetration function of IS-IS
vcs与verdi学习记录
随机推荐
Evolution of multi activity in different places
GBase8s数据库INTERSECT 运算符
十七、C函數指針與回調函數
地图找房的实例
逆袭黑马:数据库全栈工程师(DevDBOps)培训,把最好的课程送给您!
逻辑回归(公式推导+numpy实现)
Bigder:40/100 怎么组织一次用例评审
GBase8s数据库SET Database Object Mode 语句
架构篇(一)什么是架构
mysql 自增主键出现不连续的原因?
Unity TextMeshPro命名空间引用及组件获取
力扣练习——35 组合总和 II
Architecture (I) what is architecture
Bigder:38/100 a misoperation problem has been solved
Learn TB writing method with SPI simulation file
Bigder:40/100 how to organize a use case review
济南 章丘 科目三 资料 收集
How to carry out efficient data governance? Index management and data traceability help!
vcs与verdi学习记录
在线XML转CSV工具