Qt write custom annular FIG custom 41-

I. Introduction

Custom control is similar to FIG annular custom control pie chart, a pie chart area is accounted for show, in fact, the core region are plotted as a percentage of the pie chart to automatically calculated. The current control loop of FIG imitative FIG echart an annular control ring chart bunk, the outer annular layer of FIG., There is a layer inside the pie chart represent the equivalent of a control can be two types of accounting , so that a greater amount of information covered, but also provides a hover feature automatically highlighted, along with the following legend is also highlighted in bold, very intuitive, similar controls extensive use in many web projects.
Difficulties of this control are not drawn annular areas or pie novice will, difficulties in how to automatically calculate the precise area where the mouse, and then highlight indicates, contains a method using a mouse QPainterPath determines which area the current, it is necessary to draw Remember when QPainterPath the pie chart area, and then determine the mouseMoveEvent, the need to open the mouse capture. Controls author Amada brother ( https://blog.csdn.net/ly305750665 )

Second, the realization of functions

  • 1: You can set whether to display the title text + title + height + title font size
  • 2: you can set whether to display the legend legend font height + + Legend
  • 3: set the background color text color + + Color Highlight Color logo +
  • 4: may be disposed circularly cylindrical intermediate color + + Color Color inner circle
  • 5: + may be disposed within outer circle data set data set
  • 6: Hover over the highlighted area and highlight text
  • 7: each zone can be set corresponding to the text description color + + percentage
  • 8: support direct text string to set the percentage of collections and collections

Third, renderings

Fourth, the header file code

#ifndef CUSTOMRING_H
#define CUSTOMRING_H

/**
 * 自定义环形图控件 整理:feiyangqingyun(QQ:517216493) 2019-7-28
 * 原作者:雨田哥(QQ:3246214072)
 * 1:可设置是否显示标题+标题文字+标题高度+标题字号
 * 2:可设置是否显示图例+图例高度+图例字号
 * 3:可设置背景颜色+文字颜色+高亮颜色+标识颜色
 * 4:可设置外圆颜色+中间圆颜色+内圆颜色
 * 5:可设置外圆数据集合+内圆数据集合
 * 6:鼠标悬停突出显示区域并高亮显示文字
 * 7:每个区域都可设置对应的颜色+文字描述+百分比
 * 8:支持直接字符串设置文字集合和百分比集合
 */

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

{
    Q_OBJECT
    Q_PROPERTY(bool showTitle READ getShowTitle WRITE setShowTitle)
    Q_PROPERTY(int titleHeight READ getTitleHeight WRITE setTitleHeight)
    Q_PROPERTY(int titleFontSize READ getTitleFontSize WRITE setTitleFontSize)
    Q_PROPERTY(QString title READ getTitle WRITE setTitle)

    Q_PROPERTY(bool showLegend READ getShowLegend WRITE setShowLegend)
    Q_PROPERTY(int legendHeight READ getLegendHeight WRITE setLegendHeight)
    Q_PROPERTY(int legendFontSize READ getLegendFontSize WRITE setLegendFontSize)

    Q_PROPERTY(QColor bgColor READ getBgColor WRITE setBgColor)
    Q_PROPERTY(QColor textColor READ getTextColor WRITE setTextColor)
    Q_PROPERTY(QColor highColor READ getHighColor WRITE setHighColor)
    Q_PROPERTY(QColor flagColor READ getFlagColor WRITE setFlagColor)

    Q_PROPERTY(QColor outCircleColor READ getOutCircleColor WRITE setOutCircleColor)
    Q_PROPERTY(QColor midCircleColor READ getMidCircleColor WRITE setMidCircleColor)
    Q_PROPERTY(QColor inCircleColor READ getInCircleColor WRITE setInCircleColor)

    Q_PROPERTY(QString outPieInfos READ getOutPieInfos WRITE setOutPieInfos)
    Q_PROPERTY(QString inPieInfos READ getInPieInfos WRITE setInPieInfos)

public:
    struct RingData {
        int offset;         //鼠标移上去往外边突出显示的偏移距离
        int percent;        //百分比
        QColor color;       //背景色
        QString text;       //文本
        QPainterPath path;  //区域路径

        RingData()
        {
            offset = 0;
            percent = 0;
            color = QColor(0, 192, 133);
            text = "";
        }
    };

    CustomRing(QWidget *parent = 0);
    ~CustomRing();

protected:
    void mouseMoveEvent(QMouseEvent *event);
    void paintEvent(QPaintEvent *);
    void drawBg(QPainter *painter);
    void drawOutCircle(QPainter *painter);
    void drawOutPie(QPainter *painter, qreal scale, QPoint center);
    void drawMidCircle(QPainter *painter);
    void drawInPie(QPainter *painter, qreal scale, QPoint center);
    void drawInCircle(QPainter *painter);
    void drawTitle(QPainter *painter);
    void drawLegendText(QPainter *painter, qreal scale);

private:
    bool showTitle;             //显示标题
    int titleHeight;            //标题高度
    int titleFontSize;          //标题字号
    QString title;              //标题

    bool showLegend;            //显示图例
    int legendHeight;           //图例高度
    int legendFontSize;         //图例字号

    QColor bgColor;             //背景颜色
    QColor textColor;           //文字颜色
    QColor highColor;           //高亮颜色
    QColor flagColor;           //标题左侧标识颜色

    QColor outCircleColor;      //外圆颜色
    QColor midCircleColor;      //中间圆颜色
    QColor inCircleColor;       //里边圆颜色

    QString outPieInfos;        //外边饼图数据
    QString inPieInfos;         //里边饼图数据
    QList<QColor> outPieColors; //饼图颜色集合,在设置字符串时候用
    QList<QColor> inPieColors;  //饼图颜色集合,在设置字符串时候用

    QList<RingData> outPieInfo; //外边饼图数据
    QList<RingData> inPieInfo;  //里边饼图数据

public:
    bool getShowTitle()         const;
    int getTitleHeight()        const;
    int getTitleFontSize()      const;
    QString getTitle()          const;

    bool getShowLegend()        const;
    int getLegendHeight()       const;
    int getLegendFontSize()     const;

    QColor getBgColor()         const;
    QColor getTextColor()       const;
    QColor getHighColor()       const;
    QColor getFlagColor()       const;

    QColor getOutCircleColor()  const;
    QColor getMidCircleColor()  const;
    QColor getInCircleColor()   const;

    QString getOutPieInfos()    const;
    QString getInPieInfos()     const;

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

public Q_SLOTS:
    //显示标题+标题栏高度+标题字号+标题文字
    void setShowTitle(bool showTitle);
    void setTitleHeight(int titleHeight);
    void setTitleFontSize(int titleFontSize);
    void setTitle(const QString &title);

    //显示图例+图例高度+图例字号
    void setShowLegend(bool showLegend);
    void setLegendHeight(int legendHeight);
    void setLegendFontSize(int legendFontSize);

    //设置背景颜色+文字颜色+高亮颜色+标识颜色
    void setBgColor(const QColor &bgColor);
    void setTextColor(const QColor &textColor);
    void setHighColor(const QColor &highColor);
    void setFlagColor(const QColor &flagColor);

    //设置外圆颜色+中间圆颜色+里边圆颜色
    void setOutCircleColor(const QColor &outCircleColor);
    void setMidCircleColor(const QColor &midCircleColor);
    void setInCircleColor(const QColor &inCircleColor);

    //字符串形式设置数据
    void setOutPieInfos(const QString &outPieInfos);
    void setInPieInfos(const QString &inPieInfos);

    //设置颜色集合
    void setOutPieColors(const QList<QColor> &outPieColors);
    void setInPieColors(const QList<QColor> &inPieColors);

    //清空+设置饼图数据
    void clearOutPie();
    void clearInPie();
    void appendOutPie(const RingData &data);
    void appendInPie(const RingData &data);
};

#endif // CUSTOMRING_H

Fifth, the core code

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

    int titleHeight = showTitle ? this->titleHeight : 0;
    int legendHeight = showLegend ? this->legendHeight : 0;
    QRect rect(0, titleHeight, this->width(), this->height() - titleHeight - legendHeight);
    int side = qMin(rect.width(), rect.height());
    qreal scale = side / 200.0;

    //绘制背景
    drawBg(&painter);

    //平移坐标轴中心,等比例缩放
    painter.save();
    painter.translate(rect.center());
    painter.scale(scale, scale);

    //绘制外圆背景
    drawOutCircle(&painter);
    //绘制外层饼图
    drawOutPie(&painter, scale, rect.center());
    //绘制中间圆
    drawMidCircle(&painter);
    //绘制里层饼图
    drawInPie(&painter, scale, rect.center());
    //绘制里边圆
    drawInCircle(&painter);

    painter.restore();

    //重新等比例缩放,绘制文字,文字放在后面绘制是为了不被圆遮挡
    painter.scale(scale, scale);

    //绘制标题
    if (showTitle) {
        drawTitle(&painter);
    }

    //绘制图例文字
    if (showLegend) {
        drawLegendText(&painter, scale);
    }
}

void CustomRing::drawBg(QPainter *painter)
{
    painter->save();
    painter->setPen(Qt::NoPen);
    painter->setBrush(bgColor);
    painter->drawRoundedRect(this->rect(), 5, 5);
    painter->restore();
}

void CustomRing::drawOutCircle(QPainter *painter)
{
    int radius = 90;
    painter->save();
    painter->setPen(Qt::NoPen);
    painter->setBrush(outCircleColor);
    painter->drawEllipse(QPoint(0, 0), radius, radius);
    painter->restore();
}

void CustomRing::drawMidCircle(QPainter *painter)
{
    int radius = 50;
    painter->save();
    painter->setPen(Qt::NoPen);
    painter->setBrush(midCircleColor);
    painter->drawEllipse(QPoint(0, 0), radius, radius);
    painter->restore();
}

void CustomRing::drawInCircle(QPainter *painter)
{
    int radius = 10;
    painter->save();
    painter->setPen(Qt::NoPen);
    painter->setBrush(inCircleColor);
    painter->drawEllipse(QPoint(0, 0), radius, radius);
    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.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/11298984.html