QT c++ 样式 设置 标签(QLabel)的渐变色美化

上一篇文章中描述了按钮的纯色,本文描述标签的渐变色美化。

1.头文件

#ifndef WIDGET_H

#define WIDGET_H

#include <QWidget>

//#include "CustomButton.h"
#include <QVBoxLayout>
#include <QLinearGradient>
#include <QLabel>
#include <QTimer>

QT_BEGIN_NAMESPACE

namespace Ui {

class Widget;

}

QT_END_NAMESPACE

class Widget : public QWidget

{

Q_OBJECT

public:

Widget(QWidget *parent = nullptr);

~Widget();

void initUI();

private:

Ui::Widget *ui;

// CustomButton *button;

// QPushButton *button2;

QLabel *label;

QTimer *timer;

int counter=0;//计时器

};

2.cpp文件

#include "widget.h"

#include "ui_widget.h"

Widget::Widget(QWidget *parent)

: QWidget(parent)

, ui(new Ui::Widget)

{

ui->setupUi(this);

initUI();

}

Widget::~Widget()

{

delete ui;

}

void Widget::initUI()

{

QVBoxLayout * mainlayout = new QVBoxLayout(this);//指定布局属于啥组件

QLabel *label=new QLabel(this);

timer=new QTimer(this);

timer->start(1000);//定时1秒

QObject::connect(timer, &QTimer::timeout,

**label,this**()//lambda函数

{ counter++ ;

if(1==counter%2 )

//qDebug("pressed!");

{

label->setStyleSheet(" border-radius: 15px;"

"border: 2px solid rgb(100, 120, 100); "

"background-color: qlineargradient(x1: 0, y1: 0,"

" x2: 1, y2: 1, "

"stop: 0 #ffffff,"

"stop: 0.3 #bbeebb,"

"stop: 1 #4f4f4f);"

);//暗

}

else

{

label->setStyleSheet(" border-radius: 15px;"

"border: 2px solid rgb(100, 120, 100); "

"background-color: qlineargradient(x1: 0, y1: 0, x2: 1, y2: 1, "

"stop: 0 #ffffff,"

"stop: 0.3 #43ff43,"

"stop: 1 #669066);"

);//亮绿色

}

});

label->setFixedWidth(30);
label->setFixedHeight(30);

mainlayout->addWidget(label);//指定布局包含啥组件

this->setLayout(mainlayout);

}

//效果:

相关推荐
金色熊族3 分钟前
在Qt中使用asan检测内存泄漏(二)--检查依赖库的内存使用
linux·qt·asan
zhangx1234_4 分钟前
javaEE 多线程4
java·开发语言
大陈子编程10 分钟前
C++ 背包九讲:从入门到精通的动态规划专题
开发语言·c++·动态规划
qq_4017004117 分钟前
Qt 组播(Multicast)实战详解
开发语言·qt
Sapphire~31 分钟前
odoo-095 视图的重写规则(全部重写还是部分改写)
开发语言·javascript·ecmascript
利威尔xu32 分钟前
Go GMP 调度器 10 个高频追问:3 个线上故障让 P99 从 80ms 飙到 2s、10 万 goroutine 泄漏
开发语言·后端·面试·性能优化·golang
中电金信38 分钟前
从业务调优到分层缓存:DeepSeek V4 Flash的PPU实践
java·开发语言
梦影_41 分钟前
MySQL进阶教程(二)—— 储存过程与触发器(下)
开发语言·数据库·后端·sql·mysql·oracle·数据库开发
lg_cool_1 小时前
Python 代码打包
开发语言·python
木雷坞1 小时前
PHP两项9.2分漏洞:Docker容器更新与版本核验教程
开发语言·docker·php