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); } //效果: ![](https://i-blog.csdnimg.cn/direct/b14806d003854380aaf04acf0980d106.png) ![](https://i-blog.csdnimg.cn/direct/46de3327eab54f21ad493ccd4c376c73.png)

相关推荐
没有梦想的咸鱼185-1037-16637 分钟前
基于R语言机器学习方法在生态经济学领域中的实践技术应用
开发语言·机器学习·数据分析·r语言
向上的车轮31 分钟前
基于go语言的云原生TodoList Demo 项目,验证云原生核心特性
开发语言·云原生·golang
The Chosen One98532 分钟前
C++ : AVL树-详解
开发语言·c++
zzyzxb37 分钟前
std::enable_shared_from_this
c++
SNAKEpc1213843 分钟前
QML和Qt Quick
c++·qt
PH_modest43 分钟前
【Qt跬步积累】—— 初识Qt
开发语言·qt
hansang_IR1 小时前
【题解】洛谷 P4286 [SHOI2008] 安全的航线 [递归分治]
c++·数学·算法·dfs·题解·向量·点积
GanGuaGua1 小时前
Linux系统:线程的互斥和安全
linux·运维·服务器·c语言·c++·安全
怀旧,1 小时前
【C++】18. 红⿊树实现
开发语言·c++
lsnm1 小时前
【LINUX网络】IP——网络层
linux·服务器·网络·c++·网络协议·tcp/ip