Write custom Qt 58- FIG symmetrical histogram

I. Introduction

This control is also non-original control code from the reference line of symmetry as the name suggests is the average canvas two parts, the value will be set automatically according to the height of half the canvas is drawn as a reference height, and then to increase the dynamic transition effect, a bit playing sound spectrum is similar to the effect of time, usually with a plurality of straight symmetry FIG combined into a plurality of control to achieve the effect, look more beautiful, the background color gradient may be provided, the color of the columnar bar may be provided on their own.

Second, the realization of functions

  • 1: you can set the maximum / minimum / current values
  • 2: a setting step of each transition
  • 3: the interval may be provided between the item
  • 4: gradient can set the background color
  • 5: the color of the columnar bar may be provided

Third, renderings

Fourth, the header file code

#ifndef WAVEDOUBLE_H
#define WAVEDOUBLE_H

/**
 * 直方对称图控件 作者:feiyangqingyun(QQ:517216493) 2016-11-6
 * 1:可设置最大值/最小值/当前值
 * 2:可设置每次过渡的步长
 * 3:可设置item之间的间隔
 * 4:可设置渐变的背景颜色
 * 5:可设置柱状条的颜色
 */

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

{
    Q_OBJECT    
    Q_PROPERTY(int minValue READ getMinValue WRITE setMinValue)
    Q_PROPERTY(int maxValue READ getMaxValue WRITE setMaxValue)
    Q_PROPERTY(int value READ getValue WRITE setValue)

    Q_PROPERTY(double step READ getStep WRITE setStep)
    Q_PROPERTY(int space READ getSpace WRITE setSpace)

    Q_PROPERTY(QColor bgColorStart READ getBgColorStart WRITE setBgColorStart)
    Q_PROPERTY(QColor bgColorEnd READ getBgColorEnd WRITE setBgColorEnd)
    Q_PROPERTY(QColor barColor READ getBarColor WRITE setBarColor)

public:
    explicit WaveDouble(QWidget *parent = 0);
    ~WaveDouble();

protected:
    void paintEvent(QPaintEvent *);
    void drawBg(QPainter *painter);
    void drawBar(QPainter *painter);

private:    
    int minValue;                   //最小值
    int maxValue;                   //最大值
    int value;                      //目标值

    int step;                       //步长
    int space;                      //间距

    QColor bgColorStart;            //背景渐变开始颜色
    QColor bgColorEnd;              //背景渐变结束颜色
    QColor barColor;                //柱状条颜色

    double currentValue;            //当前值
    bool reverse;                   //是否倒退
    QTimer *timer;                  //绘制定时器

private slots:
    void updateValue();
    void stop();

public:    
    int getMinValue()               const;
    int getMaxValue()               const;
    int getValue()                  const;

    int getStep()                   const;
    int getSpace()                  const;

    QColor getBgColorStart()        const;
    QColor getBgColorEnd()          const;
    QColor getBarColor()            const;

    QSize sizeHint()                const;
    QSize minimumSizeHint()         const;

public Q_SLOTS:
    //设置范围值
    void setRange(int minValue, int maxValue);

    //设置最大最小值
    void setMinValue(int minValue);
    void setMaxValue(int maxValue);

    //设置目标值
    void setValue(int value);

    //设置步长
    void setStep(int step);
    //设置间距
    void setSpace(int space);

    //设置背景颜色
    void setBgColorStart(const QColor &bgColorStart);
    void setBgColorEnd(const QColor &bgColorEnd);

    //设置柱状条颜色
    void setBarColor(const QColor &barColor);

Q_SIGNALS:
    void valueChanged(int value);
};

#endif // WAVEDOUBLE_H

Fifth, the core code

void WaveDouble::paintEvent(QPaintEvent *)
{
    //绘制准备工作,启用反锯齿
    QPainter painter(this);
    painter.setRenderHints(QPainter::Antialiasing | QPainter::TextAntialiasing);

    //绘制渐变背景
    drawBg(&painter);
    //绘制柱状条块
    drawBar(&painter);
}

void WaveDouble::drawBg(QPainter *painter)
{
    painter->save();
    painter->setPen(Qt::NoPen);
    QLinearGradient bgGradient(QPointF(0, 0), QPointF(0, height()));
    bgGradient.setColorAt(0.0, bgColorStart);
    bgGradient.setColorAt(1.0, bgColorEnd);
    painter->setBrush(bgGradient);
    painter->drawRect(rect());
    painter->restore();
}

void WaveDouble::drawBar(QPainter *painter)
{
    painter->save();
    painter->setPen(Qt::NoPen);
    painter->setBrush(barColor);

    //找到中心点Y轴坐标
    double centerY = (double) height() / 2;
    //每一格递增量
    double increment = (double)(height() - 2 * space) / (maxValue - minValue);
    //找到当前值的起始和结束Y轴坐标 上下两半所以 /2
    double startY = centerY - (currentValue / 2) * increment;
    double endY = centerY + (currentValue / 2) * increment;
    QRectF barRect(QPointF(space, startY), QPointF(width() - space, endY));
    painter->drawRect(barRect);
    painter->restore();
}

Sixth, the controls described

  1. More than 150 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.13, 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 dynamic library files (dll or so, etc.) 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.
  15. Custom plug-in open dynamic library (permanent free), and the back door without any restrictions, ease of use.
  16. 26 now available version dll, which includes qt5.12.3 msvc2017 32 + 64 mingw 32 + 64 in.
  17. From time to time to increase control and improve controls, regularly updated SDK, to welcome all suggestions, thank you!
  18. Qt introductory books recommended Huo Yafei of "Qt Creator Quick Start" "Qt5 programming entry", Qt official Advanced book recommendations "C ++ GUI Qt4 programming."
  19. Highly recommended programmer self-discipline and planning book series "lying programmer" "Programmer's growth course", "grief programmer", benefited from a lifetime!
  20. SDK download link: https://pan.baidu.com/s/1A5Gd77kExm8Co5ckT51vvQ extraction code: 877p

Guess you like

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