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)

相关推荐
小莞尔3 分钟前
【51单片机】【protues仿真】 基于51单片机八路抢答器系统
c语言·开发语言·单片机·嵌入式硬件·51单片机
我是菜鸟0713号24 分钟前
Qt 中 OPC UA 通讯实战
开发语言·qt
JCBP_26 分钟前
QT(4)
开发语言·汇编·c++·qt·算法
Brookty30 分钟前
【JavaEE】线程安全-内存可见性、指令全排序
java·开发语言·后端·java-ee·线程安全·内存可见性·指令重排序
百锦再44 分钟前
[特殊字符] Python在CentOS系统执行深度指南
开发语言·python·plotly·django·centos·virtualenv·pygame
Anson Jiang44 分钟前
浏览器标签页管理:使用chrome.tabs API实现新建、切换、抓取内容——Chrome插件开发从入门到精通系列教程06
开发语言·前端·javascript·chrome·ecmascript·chrome devtools·chrome插件
会开花的二叉树1 小时前
继承与组合:C++面向对象的核心
java·开发语言·c++
潮汐退涨月冷风霜2 小时前
数字图像处理(1)OpenCV C++ & Opencv Python显示图像和视频
c++·python·opencv
长河3 小时前
Java开发者LLM实战——LangChain4j最新版教学知识库实战
java·开发语言
第七序章3 小时前
【C++STL】list的详细用法和底层实现
c语言·c++·自然语言处理·list