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)

相关推荐
艾莉丝努力练剑26 分钟前
【LeetCode&数据结构】单链表的应用——反转链表问题、链表的中间节点问题详解
c语言·开发语言·数据结构·学习·算法·leetcode·链表
还债大湿兄2 小时前
《C++内存泄漏8大战场:Qt/MFC实战详解 + 面试高频陷阱破解》
c++·qt·mfc
倔强青铜34 小时前
苦练Python第18天:Python异常处理锦囊
开发语言·python
u_topian5 小时前
【个人笔记】Qt使用的一些易错问题
开发语言·笔记·qt
珊瑚里的鱼5 小时前
LeetCode 692题解 | 前K个高频单词
开发语言·c++·算法·leetcode·职场和发展·学习方法
AI+程序员在路上5 小时前
QTextCodec的功能及其在Qt5及Qt6中的演变
开发语言·c++·qt
xingshanchang6 小时前
Matlab的命令行窗口内容的记录-利用diary记录日志/保存命令窗口输出
开发语言·matlab
Risehuxyc6 小时前
C++卸载了会影响电脑正常使用吗?解析C++运行库的作用与卸载后果
开发语言·c++
AI视觉网奇6 小时前
git 访问 github
运维·开发语言·docker
不知道叫什么呀6 小时前
【C】vector和array的区别
java·c语言·开发语言·aigc