Qt to write custom controls 37- illuminated pushbuttons (breathing pain)

I. Introduction

This control is used to write good morning, several people have been authorized to open this control over the code, such as the Moulin Rouge chubby, this control is not from the real needs, but merely a whim, like the stars blinking, the more edge gets weaker timer dynamically change the brightness of the light-emitting edge, an effect of breathing, pain will breathe another name, to see this song, reminds me of ex-girlfriend, hey! For too long!
The principle is to use a substantially tapered gradient QRadialGradient, then the timer value change the transparency gradient brush color, cause respiratory effects. Qt provides a good variety of gradient brush, very useful, you can perform the brush area, and then proportional interpolation, the interpolation corresponding to the specified color, so use the very rich.

Second, the realization of functions

  • 1: breath interval can be set
  • 2: a transparent color may be provided step gradient
  • 3: set the background color

Third, renderings

Fourth, the header file code

#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

Fifth, the core code

#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();
    }
}

Sixth, the controls described

  1. More than 149 exquisite control, covers a variety of dashboards, progress bar, the progress of the ball, compass, graphs, scales, thermometers, navigation bar, navigation bar, flatui, highlight the button, slide the selector, the lunar calendar and so on. Qwt far more than the number of controls integration.
  2. Each class can be independently as a separate control, zero coupling each control file and a header file to achieve a code amount, independent of other files to facilitate individual control integrated into the project source code form, less. qwt interlocking control class, highly coupled, want to use one of the controls, must contain all the code.
  3. Write all pure Qt, QWidget + QPainter to draw, to support any Qt version Qt4.6 Qt5.12, support for mingw, msvc, gcc compiler, etc., support any operating system such as windows + linux + mac + embedded linux, which does not garbled can be directly integrated into Qt Creator, a built-in controls and use the same, most of the effects can be as long as several properties are set, very convenient.
  4. DEMO separate source containing the control corresponding to each control has a convenient reference. It also provides integrated use of all controls a DEMO.
  5. Source code for each control has detailed Chinese annotation, are prepared in accordance with unified design specifications, easy to learn to write custom controls.
  6. Each control default color and demo corresponding color is very beautiful.
  7. More than 130 visible control, six invisible control.
  8. Portion control provides a variety of styles style selection, multiple choice style indicator.
  9. All controls changes adaptive stretched form.
  10. Integrated design custom attribute that supports drag design, WYSIWYG support the import and export in xml format.
  11. Activex control that comes with demo, all controls can be run directly in the browser ie.
  12. Fontawesome integrated graphics font + Alibaba iconfont collection of hundreds of graphic fonts, font fun graphic brings.
  13. All controls and finally generate a dll dynamic library files, can be integrated directly into qtcreator designed for use in drag.
  14. Already qml version, the latter will consider a pyqt version, if the user is in great demand then.

Seven, SDK download

  • SDK download link: https://pan.baidu.com/s/1A5Gd77kExm8Co5ckT51vvQ extraction code: 877p
  • Download link is included in the various versions of the dynamic library files, header files of all the controls, the use of demo, custom controls + properties designer.
  • Open plug-in custom dynamic library dll use (permanent free), and the back door without any restrictions, ease of use.
  • 26 now available version dll, which includes qt5.12.3 msvc2017 32 + 64 mingw 32 + 64 in.
  • From time to time to increase control and improve controls, regularly updated SDK, to welcome all suggestions, thank you!
  • widget versions (QQ: 517216493) qml versions (QQ: 373955953) sambong camel (QQ: 278969898).
  • Qt's advanced column know almost Taoge road https://zhuanlan.zhihu.com/TaoQt
  • Welcome concern public micro-channel number] [efficient programmers, content C ++ / Python, learning, writing skills, popular technology, career development, a lot of dry goods, benefits a lot!

Guess you like

Origin www.cnblogs.com/feiyangqingyun/p/11241105.html