Qt实用技巧:会呼吸的痛(呼吸点/呼吸灯)(转载)

Qt实用技巧:会呼吸的痛(呼吸点/呼吸灯)

+传送门http://blog.csdn.net/qq21497936/article/details/78747244

对萨芬的萨

        

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>  
#endif  
  
class QDESIGNER_WIDGET_EXPORT LightPoint : public QWidget  
#else  
class 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

#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>  
#endif  
  
class QDESIGNER_WIDGET_EXPORT LightPoint : public QWidget  
#else  
class 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

[cpp] view plain copy
#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();  
    }  
}  




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值