当前位置:网站首页>QT notes - unpolish() and polish() of QT dynamic attributes
QT notes - unpolish() and polish() of QT dynamic attributes
2022-07-22 18:12:00 【Cool breeze in the old street °】
Changing styles dynamically Dynamic attributes , For example, clicking the button can change the style of the button : From red -> Blue -> green
Let's take a look at this function :
bool QObject::setProperty(const char *name, const QVariant &value)
What does this function do ?
For one based on QObject For controls , Can pass setProperty To set properties
What are its parameters ?
const char *name: That is, the custom attribute name , Be careful not to have the same name as the default property of the control
const QVariant &value: The value of this attribute
See this link for details :QT property Application of attributes
How to use it for this article ?
Our needs : Dynamically change the style of the button , Then it will play a great role
1. We need to create a new class , stay ui Put one in it pushButton, Then set the style for the button
Right click on our pushButton Click style sheet , The content is :
The first line is the initial color ,PropertyTest The new class name for us ,borderColor For the properties we need to set ,“=” The following is the value of the attribute ,btn For us pushButton Of 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, Declare the slot function that clicks the button
private slots:
void changeBorderColor(); // Change the color of the attribute
private:
int m_iTest; // The number of hits
3. Connect our slot function
connect(ui.btn, &QPushButton::clicked, this, &PropertyTest::changeBorderColor);
4. Define the contents of our slot function
void PropertyTest::changeBorderColor()
{
if (m_iTest % 3 == 0)
{
setProperty("borderColor", "red");
}
else if (m_iTest % 3 == 1)
{
setProperty("borderColor", "green");
}
else
{
setProperty("borderColor", "blue");
}
// There are two ways to update styles manually The first one is
//ui.btn->style()->unpolish(ui.btn); // Clean up the previous style
//ui.btn->style()->polish(ui.btn); // Used to add new styles
//ui.btn->update();
// The second kind
ui.btn->setStyle(QApplication::style());
m_iTest++;
}
effect :
The second method :
1. The first step of the same method
2. We need to add inheritance from QObject Used in Q_PROPERTY Macro instructions
What does this macro do ?
Look at this example :
Q_PROPERTY(QString borderColor READ getBorderColor WRITE setBorderColor)
basis Q_PROPERTY
( Parameter type Parameter name READ Get attribute value function WRITE Set attribute value function )
Parameter type :QString
Parameter name :borderColor
Get attribute value function :getBorderColor
Set attribute value function :setBorderColor
See specific macro links :Q_PROPERTY Introduce
3. Define the read-write member functions we write in macros and Save the set color
public:
void setBorderColor(const QString& strBorderColor) {
m_strBorderColor = strBorderColor; }
QString getBorderColor() {
return m_strBorderColor; }
private:
QString m_strBorderColor;
int m_iTest;
4. The third step of the same method
5. Define our slot function
void PropertyTest::changeBorderColor()
{
if (m_iTest % 3 == 0)
{
setBorderColor("red");
}
else if (m_iTest % 3 == 1)
{
setBorderColor("green");
}
else
{
setBorderColor("blue");
}
// Update styles manually
//ui.btn->style()->unpolish(ui.btn);
//ui.btn->style()->polish(ui.btn);
//ui.btn->update();
ui.btn->setStyle(QApplication::style());
m_iTest++;
}
The effect is the same as the first
Complete code :
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");
}
// Update styles manually
//ui.btn->style()->unpolish(ui.btn);
//ui.btn->style()->polish(ui.btn);
//ui.btn->update();
ui.btn->setStyle(QApplication::style());
m_iTest++;
}
Reference blog :
Qt Dynamic properties of unpolish() and polish()
QT property Application of attributes
Q_PROPERTY Introduce
边栏推荐
- zmq无锁队列的浅解
- Aruba学习笔记04-Web UI --Configuration面板介绍
- Super practical transformation strategy: 2022 central state-owned enterprise cloud native landing practical guide was officially released
- 会议OA项目之会议发布功能
- Learn TB writing method with SPI simulation file
- 逆袭黑马:数据库全栈工程师(DevDBOps)培训,把最好的课程送给您!
- Hblock盘活企业级存储市场
- [case sharing] configure the routing penetration function of IS-IS
- 路由协议是什么
- 基于攻防博弈的网络防御决策方法研究综述
猜你喜欢
pushgateway安装及Prometheus配置
学IT,你后悔了么?
云原生强势发展,企业该如何抓住业务机遇
[exception] generate guid and datetime, import test data to the database
逻辑回归(公式推导+numpy实现)
Teach you to write makefile files from 0
Oracle statement adjustment
Bigder:38/100 a misoperation problem has been solved
在线XML转CSV工具
Abnormal understanding and learning
随机推荐
Excel import export controller
shell脚本使用expect自动化交互登录远程主机进行批量关机
go 切片,集合简单讲解
Detailed explanation of bokeh parameter setting
GBase8sUNION ALL 运算符
Gbase8s database minus operator
web3分享
zmq无锁队列的浅解
Gbase8s database comparison performed by database objects
[bug] datetime format failed
[exception] generate guid and datetime, import test data to the database
Gbase8s database union operator
QT笔记——QtXml
1.4.1 好多好多道符(cin/cout解绑,O3优化)
Gbase8s database restrictions on set collection
力扣练习——21 有序矩阵中的第 k 个最小数组和
路由协议是什么
力扣练习——31 有效的井字游戏
mysql 自增主键出现不连续的原因?
GBase8s数据库SET Database Object Mode 语句