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);

}

//效果:

相关推荐
倒头就睡的小比特2 天前
算法竞赛C++常用的STL
c++·算法
weilx12342 天前
C++笔记-文件IO-<fcntl.h>
c++
小羊没烦恼!2 天前
初探性能优化——2个月到4小时的性能提升
java·开发语言·windows·算法·c#
伞伞悦读2 天前
【第38期】Python 模块与包详解:import、from、模块搜索路径、包结构和 __init__
开发语言·python
Smileyqp沛沛2 天前
前端?C++ ?较大差异基础罗列
c++·基础·前端转c++
C语言小火车2 天前
C/C++ 为什么需要编译器?
开发语言·c++
旖旎夜光2 天前
力控面试题 01.01: 判定字符是否唯一(位运算) —— 题解
c++·学习·算法·leetcode·力控
吞下星星的少年·-·2 天前
C++ 萌新语法入门篇
c++·算法比赛
霍霍的袁2 天前
【C++】map 和 set 的使用 | 从用法到底层
开发语言·c++·学习·visual studio
another heaven2 天前
【算法/C++ MD5算法能否逆解码?原理、C++实现与同类哈希算法对比】
c++·算法·哈希算法