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 小时前
软件解耦与扩展的利器:基于C++与C#的插件式开发实践
c++
知远同学4 小时前
Anaconda的安装使用(为python管理虚拟环境)
开发语言·python
小徐Chao努力4 小时前
【Langchain4j-Java AI开发】09-Agent智能体工作流
java·开发语言·人工智能
CoderCodingNo4 小时前
【GESP】C++五级真题(贪心和剪枝思想) luogu-B3930 [GESP202312 五级] 烹饪问题
开发语言·c++·剪枝
kylezhao20195 小时前
第1章:第一节 开发环境搭建(工控场景最优配置)
开发语言·c#
啃火龙果的兔子5 小时前
JavaScript 中的 Symbol 特性详解
开发语言·javascript·ecmascript
热爱专研AI的学妹5 小时前
数眼搜索API与博查技术特性深度对比:实时性与数据完整性的核心差异
大数据·开发语言·数据库·人工智能·python
Mr_Chenph5 小时前
Miniconda3在Windows11上和本地Python共生
开发语言·python·miniconda3
阿狸远翔5 小时前
Protobuf 和 protoc-gen-go 详解
开发语言·后端·golang
永远前进不waiting5 小时前
C复习——1
c语言·开发语言