Qt实用技巧:会呼吸的痛(呼吸点/呼吸灯)
来源:互联网 发布:腾讯云和阿里云哪个好 编辑:程序博客网 时间:2024/04/17 05:38
原博主博客地址:http://blog.csdn.net/qq21497936
本文章博客地址:http://blog.csdn.net/qq21497936/article/details/78747244
Qt实用技巧:会呼吸的痛(呼吸点/呼吸灯)
感谢
感谢feiyangqingyun@163.com(517216493)编写了控件并提供源码,本人对其进行详细的注释和小部分的修改,应源码提供者的要求,取名为“会呼吸的痛”。
Demo效果
Demo下载地址:http://download.csdn.net/download/qq21497936/10150349
入坑
主窗口为QWidget的子类时,不论设置QPallet和setStyleSheets设置背景图片是无法生效的,但设置颜色却可以生效。
Demo目录结构
关键代码
已经在主窗口上添加了一个QWidget
主窗口关键代码,设置布局,生成6个呼吸点
frmLightPoint::frmLightPoint(QWidget *parent) : QWidget(parent), ui(new Ui::frmLightPoint){ ui->setupUi(this); // 设置背景图片 ui->widget->setStyleSheet("QWidget#widget {background: url(:/image/bg1.jpg); }"); // 使用Grid布局 QGridLayout * pLayout = new QGridLayout(); // 生成 2行 3列 共 6 个控件 LightPoint *pLightPoint; for(int index = 0; index < 6; index++) { pLightPoint = new LightPoint(this); pLayout->addWidget(pLightPoint, index/3, index%3, 1, 1); // 控件指针加入列表,以便设置颜色,等同于 widgets.push_back(pLightPoint); widgets << pLightPoint; } // 设置布局到主窗口上的QWidget ui->widget->setLayout(pLayout); // 颜色列表 QList<QColor> colors; colors << "#47A4E9" << "#00B17D" << "#D64D54" << "#DEAF39" << "#A279C5" << "#009679"; // 循环设置颜色 for (int index = 0; index < widgets.count(); index++) { widgets.at(index)->setBgColor(colors.at(index)); }}
呼吸点控件头文件 lightpoint.h
#ifndef LIGHTPOINT_H#define LIGHTPOINT_H#include <QWidget>#ifdef quc#if (QT_VERSION < QT_VERSION_CHECK(5,7,0))#include <QtDesigner/QDesignerExportWidget>#else#include <QtUiPlugin/QDesignerExportWidget>#endifclass QDESIGNER_WIDGET_EXPORT LightPoint : public QWidget#elseclass LightPoint : public QWidget#endif{ Q_OBJECT Q_PROPERTY(int step READ getStep WRITE setStep) Q_PROPERTY(int interval READ getInterval WRITE setInterval) Q_PROPERTY(QColor bgColor READ getBgColor WRITE setBgColor)public: explicit LightPoint(QWidget *parent = 0); ~LightPoint();protected: void paintEvent(QPaintEvent *) override; void drawBg(QPainter *painter);private: int step; //颜色透明渐变步长 int interval; //定时器间隔 QColor bgColor; //背景颜色 QTimer *timer; //绘制定时器 int offset; //偏移量 bool add; //是否增加public: int getStep() const; int getInterval() const; QColor getBgColor() const; QSize sizeHint() const; QSize minimumSizeHint() const;public slots: //设置颜色透明渐变步长 void setStep(int step); //设置定时器间隔 void setInterval(int interval); //设置背景颜色 void setBgColor(const QColor &bgColor);};#endif // LIGHTPOINT_H
呼吸点控件源代码 lightpoint.cpp
#include "lightpoint.h"#include "qpainter.h"#include "qevent.h"#include "qtimer.h"LightPoint::LightPoint(QWidget *parent) : QWidget(parent){ step = 10; interval = 100; bgColor = QColor(255, 0, 0); timer = new QTimer(this); connect(timer, SIGNAL(timeout()), this, SLOT(update())); timer->start(100); offset = 0; add = true;}LightPoint::~LightPoint(){ if (timer->isActive()) { timer->stop(); }}void LightPoint::paintEvent(QPaintEvent *){ QPainter painter(this); // 绘制准备工作 启用反锯齿 painter.setRenderHints(QPainter::Antialiasing | QPainter::TextAntialiasing); // 平移坐标轴中心, painter.translate(rect().width() / 2, rect().height() / 2); //绘制背景 drawBg(&painter);}void LightPoint::drawBg(QPainter *painter){ // 半径为当前 宽 或者 高 的一半 int radius = qMin(rect().width(), rect().height())/2; // 保存当前painter painter->save(); // 以点为中心的渐变色 QRadialGradient g(QPoint(0, 0), radius); // 循环加减 (offset < 100 && add) ? (offset += step) : (add = false); (offset > 0 && !add) ? (offset -= step) : (add = true); // 按照 点范围[0.0,1.0] 对于 每点的颜色 bgColor.setAlpha( 200+offset > 255 ? 255 : 200+offset ); g.setColorAt(0.0, bgColor); bgColor.setAlpha( 140+offset); g.setColorAt(0.2, bgColor); bgColor.setAlpha( 80+offset); g.setColorAt(0.4, bgColor); bgColor.setAlpha( 20+offset >= 0 ? 20+offset : 0 ); g.setColorAt(0.6, bgColor); bgColor.setAlpha( -60+offset >= 0 ? -50+offset : 0 ); g.setColorAt(0.8, bgColor); bgColor.setAlpha( 0 ); g.setColorAt(1.0, bgColor); // 设置 画笔 图形的边界线 painter->setPen(Qt::NoPen); // 设置 画刷 画刷为 点向外辐射的渐变色 painter->setBrush(g); // 画椭圆,长=宽 为原型 painter->drawEllipse(-radius, -radius, radius * 2, radius * 2); // 回复保存的 painter->restore();}int LightPoint::getStep() const{ return this->step;}int LightPoint::getInterval() const{ return this->interval;}QColor LightPoint::getBgColor() const{ return this->bgColor;}QSize LightPoint::sizeHint() const{ return QSize(100, 100);}QSize LightPoint::minimumSizeHint() const{ return QSize(5, 5);}void LightPoint::setStep(int step){ if (this->step != step) { this->step = step; update(); }}void LightPoint::setInterval(int interval){ if (this->interval != interval) { this->interval = interval; timer->setInterval(interval); update(); }}void LightPoint::setBgColor(const QColor &bgColor){ if (this->bgColor != bgColor) { this->bgColor = bgColor; update(); }}
原博主博客地址:http://blog.csdn.net/qq21497936
本文章博客地址:http://blog.csdn.net/qq21497936/article/details/78747244
阅读全文
0 0
- Qt实用技巧:会呼吸的痛(呼吸点/呼吸灯)
- Qt实用技巧:会呼吸的痛(呼吸点/呼吸灯)
- 会呼吸的痛
- 素质教育,会呼吸的痛。
- 呼吸
- 呼吸
- 呼吸
- 会呼吸的圆
- 会呼吸的痛 梁静茹-会呼吸的痛MP3试听-会呼吸的痛歌词,会呼吸的痛歌词下载
- 想念是会呼吸的痛
- 想念是会呼吸的痛
- 思念是会呼吸的痛
- 呼吸灯的实现
- iOS 会呼吸的动画
- 呼吸灯
- 呼吸灯
- 呼吸灯
- 呼吸灯
- 2017.12.7
- 清除浮动的方法
- 12.7
- Linux添加虚拟网卡的多种方法
- DDS数据分发服务OpenDDS概述
- Qt实用技巧:会呼吸的痛(呼吸点/呼吸灯)
- 关于SpringMVC中String与Date类型转化问题
- 多线程中关键字Volatile的用法及作用
- centos安装TCP重定向工具rinetd(以安装mongodb为例)
- 1055. 集体照 (25)
- python运行shell command
- spring boot异步设计接口
- 【学习笔记】Python的输入输出
- inception-v3迁移学习