Qt編寫自定義控件37-發光按鈕(會呼吸的痛)

1、前言

這個控件是好早之前寫的,已經受權過好幾我的開源過此控件代碼,好比紅磨坊小胖,此控件並非來源於真實需求,而僅僅是突發奇想,相似於星星的閃爍,越到邊緣愈來愈淡,定時器動態改變邊緣發光的亮度,產生呼吸的效果,別名叫會呼吸的痛,看到這個歌名,又讓我想起了前女朋友,哎!久久不能忘懷! 大體的原理就是使用了錐形漸變QRadialGradient,而後定時器改變該漸變畫刷的顏色的透明度值,產生呼吸效果。Qt中提供了好多種漸變畫刷,很是有用,能夠執行畫刷的區域,而後等比例插值,指定插值對應的顏色,這樣使用起來就很是的豐富了。linux

2、實現的功能

  • 1:可設置呼吸間隔
  • 2:可設置顏色透明漸變步長
  • 3:可設置背景顏色

3、效果圖

4、頭文件代碼

#ifndef LIGHTPOINT_H
#define LIGHTPOINT_H

/**
 * 呼吸點控件 做者:feiyangqingyun(QQ:517216493) 2017-11-27
 * 1:可設置呼吸間隔
 * 2:可設置顏色透明漸變步長
 * 3:可設置背景顏色
 */

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

5、核心代碼

#pragma execution_character_set("utf-8")

#include "lightpoint.h"
#include "qpainter.h"
#include "qevent.h"
#include "qtimer.h"
#include "qdebug.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 *)
{
    int width = this->width();
    int height = this->height();
    int side = qMin(width, height);

    //繪製準備工做,啓用反鋸齒,平移座標軸中心,等比例縮放
    QPainter painter(this);
    painter.setRenderHints(QPainter::Antialiasing | QPainter::TextAntialiasing);
    painter.translate(width / 2, height / 2);
    painter.scale(side / 200.0, side / 200.0);

    //繪製背景
    drawBg(&painter);
}

void LightPoint::drawBg(QPainter *painter)
{
    int radius = 99;
    painter->save();

    QRadialGradient g(QPoint(0, 0), radius);

    (offset < 70 && add) ? (offset += step) : (add = false);
    (offset > 0 && !add) ? (offset -= step) : (add = true);

    bgColor.setAlpha(255);
    g.setColorAt(0.1, bgColor);
    bgColor.setAlpha(100 + offset);
    g.setColorAt(0.3, bgColor);
    bgColor.setAlpha(50 + offset);
    g.setColorAt(0.6, 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();
    }
}

6、控件介紹

  1. 超過149個精美控件,涵蓋了各類儀表盤、進度條、進度球、指南針、曲線圖、標尺、溫度計、導航條、導航欄,flatui、高亮按鈕、滑動選擇器、農曆等。遠超qwt集成的控件數量。
  2. 每一個類均可以獨立成一個單獨的控件,零耦合,每一個控件一個頭文件和一個實現文件,不依賴其餘文件,方便單個控件以源碼形式集成到項目中,較少代碼量。qwt的控件類環環相扣,高度耦合,想要使用其中一個控件,必須包含全部的代碼。
  3. 所有純Qt編寫,QWidget+QPainter繪製,支持Qt4.6到Qt5.12的任何Qt版本,支持mingw、msvc、gcc等編譯器,支持任意操做系統好比windows+linux+mac+嵌入式linux等,不亂碼,可直接集成到Qt Creator中,和自帶的控件同樣使用,大部分效果只要設置幾個屬性便可,極爲方便。
  4. 每一個控件都有一個對應的單獨的包含該控件源碼的DEMO,方便參考使用。同時還提供一個全部控件使用的集成的DEMO。
  5. 每一個控件的源代碼都有詳細中文註釋,都按照統一設計規範編寫,方便學習自定義控件的編寫。
  6. 每一個控件默認配色和demo對應的配色都很是精美。
  7. 超過130個可見控件,6個不可見控件。
  8. 部分控件提供多種樣式風格選擇,多種指示器樣式選擇。
  9. 全部控件自適應窗體拉伸變化。
  10. 集成自定義控件屬性設計器,支持拖曳設計,所見即所得,支持導入導出xml格式。
  11. 自帶activex控件demo,全部控件能夠直接運行在ie瀏覽器中。
  12. 集成fontawesome圖形字體+阿里巴巴iconfont收藏的幾百個圖形字體,享受圖形字體帶來的樂趣。
  13. 全部控件最後生成一個dll動態庫文件,能夠直接集成到qtcreator中拖曳設計使用。
  14. 目前已經有qml版本,後期會考慮出pyqt版本,若是用戶需求量很大的話。

7、SDK下載

  • SDK下載連接:https://pan.baidu.com/s/1A5Gd77kExm8Co5ckT51vvQ 提取碼:877p
  • 下載連接中包含了各個版本的動態庫文件,全部控件的頭文件,使用demo,自定義控件+屬性設計器。
  • 自定義控件插件開放動態庫dll使用(永久免費),無任何後門和限制,請放心使用。
  • 目前已提供26個版本的dll,其中包括了qt5.12.3 msvc2017 32+64 mingw 32+64 的。
  • 不按期增長控件和完善控件,不按期更新SDK,歡迎各位提出建議,謝謝!
  • widget版本(QQ:517216493)qml版本(QQ:373955953)三峯駝(QQ:278969898)。
  • 濤哥的知乎專欄 Qt進階之路 https://zhuanlan.zhihu.com/TaoQt
  • 歡迎關注微信公衆號【高效程序員】,C++/Python、學習方法、寫做技巧、熱門技術、職場發展等內容,乾貨多多,福利多多!
相關文章
相關標籤/搜索