zoukankan      html  css  js  c++  java
  • Qt编写自定义控件2-进度条标尺

    前言

    进度条标尺控件的应用场景一般是需要手动拉动进度,上面有标尺可以看到当前进度,类似于qslider控件,其实就是qslider+qprogressbar的杂交版本,不过我才用的是纯qpainter绘制的方式,这样非常灵活可靠,继承自qwidget,这个控件属于标尺类控件中的一个,就是在刻度尺控件基础上增加了鼠标按下拖动进度的功能。

    实现的功能

    • 1:可设置精确度(小数点后几位)和间距
    • 2:可设置背景色/线条颜色
    • 3:可设置长线条步长及短线条步长
    • 4:可启用动画及设置动画步长
    • 5:可设置范围值
    • 6:可设置进度颜色
    • 7:支持负数刻度值
    • 8:可设置标尺在上面还是下面
    • 9:支持直接按下定位进度

    效果图

    头文件代码

    #ifndef RULERLINE_H
    #define RULERLINE_H
    
    /**
     * 进度标尺控件 作者:feiyangqingyun(QQ:517216493) 2019-4-11
     * 1:可设置精确度(小数点后几位)和间距
     * 2:可设置背景色/线条颜色
     * 3:可设置长线条步长及短线条步长
     * 4:可启用动画及设置动画步长
     * 5:可设置范围值
     * 6:可设置进度颜色
     * 7:支持负数刻度值
     * 8:可设置标尺在上面还是下面
     * 9:支持直接按下定位进度
     */
    
    #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 RulerProgress : public QWidget
    #else
    class RulerProgress : public QWidget
    #endif
    
    {
        Q_OBJECT    
        Q_PROPERTY(double minValue READ getMinValue WRITE setMinValue)
        Q_PROPERTY(double maxValue READ getMaxValue WRITE setMaxValue)
        Q_PROPERTY(double value READ getValue WRITE setValue)
        Q_PROPERTY(int precision READ getPrecision WRITE setPrecision)
    
        Q_PROPERTY(int longStep READ getLongStep WRITE setLongStep)
        Q_PROPERTY(int shortStep READ getShortStep WRITE setShortStep)
        Q_PROPERTY(bool rulerTop READ getRulerTop WRITE setRulerTop)
    
        Q_PROPERTY(bool animation READ getAnimation WRITE setAnimation)
        Q_PROPERTY(double animationStep READ getAnimationStep WRITE setAnimationStep)
    
        Q_PROPERTY(QColor bgColor READ getBgColor WRITE setBgColor)
        Q_PROPERTY(QColor lineColor READ getLineColor WRITE setLineColor)
        Q_PROPERTY(QColor progressColor READ getProgressColor WRITE setProgressColor)
    
    public:
        explicit RulerProgress(QWidget *parent = 0);
        ~RulerProgress();
    
    protected:
        void mousePressEvent(QMouseEvent *);
        void mouseMoveEvent(QMouseEvent *);
        void setPressedValue(QPoint pressedPoint);
        void paintEvent(QPaintEvent *);
        void drawBg(QPainter *painter);    
        void drawProgress(QPainter *painter);
        void drawRulerTop(QPainter *painter);
        void drawRulerBottom(QPainter *painter);
    
    private:    
        double minValue;                //最小值
        double maxValue;                //最大值
        double value;                   //目标值
        int precision;                  //精确度,小数点后几位
    
        int longStep;                   //长线条等分步长
        int shortStep;                  //短线条等分步长
        bool rulerTop;                  //刻度线在上面
    
        bool animation;                 //是否启用动画显示
        double animationStep;           //动画显示时步长
    
        QColor bgColor;                 //背景颜色
        QColor lineColor;               //线条颜色
        QColor progressColor;           //进度颜色
    
        bool reverse;                   //是否倒退
        double currentValue;            //当前值
        QTimer *timer;                  //定时器绘制动画
    
    private slots:
        void updateValue();
    
    public:    
        double getMinValue()            const;
        double getMaxValue()            const;
        double getValue()               const;
        int getPrecision()              const;
    
        int getLongStep()               const;
        int getShortStep()              const;
        bool getRulerTop()              const;
    
        bool getAnimation()             const;
        double getAnimationStep()       const;
    
        QColor getBgColor()             const;
        QColor getLineColor()           const;
        QColor getProgressColor()       const;
    
        QSize sizeHint()                const;
        QSize minimumSizeHint()         const;
    
    public Q_SLOTS:
        //设置范围值
        void setRange(double minValue, double maxValue);
        void setRange(int minValue, int maxValue);
    
        //设置最大最小值
        void setMinValue(double minValue);
        void setMaxValue(double maxValue);
    
        //设置目标值
        void setValue(double value);
        void setValue(int value);
    
        //设置精确度
        void setPrecision(int precision);
        //设置线条等分步长
        void setLongStep(int longStep);
        void setShortStep(int shortStep);
        //设置刻度尺在上面
        void setRulerTop(bool rulerTop);
    
        //设置是否启用动画显示
        void setAnimation(bool animation);
        //设置动画显示的步长
        void setAnimationStep(double animationStep);
    
        //设置背景颜色
        void setBgColor(const QColor &bgColor);
        //设置线条颜色
        void setLineColor(const QColor &lineColor);
        //设置进度颜色
        void setProgressColor(const QColor &progressColor);
    
    Q_SIGNALS:
        void valueChanged(double value);
    };
    
    #endif // RULERLINE_H
    
    

    核心代码

    void RulerProgress::paintEvent(QPaintEvent *)
    {
        //绘制准备工作,启用反锯齿
        QPainter painter(this);
        painter.setRenderHints(QPainter::Antialiasing | QPainter::TextAntialiasing);
    
        //绘制渐变背景
        drawBg(&painter);
        //绘制进度
        drawProgress(&painter);
        //绘制标尺
        if (rulerTop) {
            drawRulerTop(&painter);
        } else {
            drawRulerBottom(&painter);
        }
    }
    
    void RulerProgress::drawBg(QPainter *painter)
    {
        painter->save();
        painter->setPen(lineColor);
        painter->setBrush(bgColor);
        painter->drawRect(rect());
        painter->restore();
    }
    
    void RulerProgress::drawProgress(QPainter *painter)
    {
        painter->save();
        painter->setPen(Qt::NoPen);
        painter->setBrush(progressColor);
    
        double length = width();
        double increment = length / (maxValue - minValue);
        double initX = (currentValue - minValue) * increment;
    
        QRect rect(0, 0, initX, height());
        painter->drawRect(rect);
        painter->restore();
    }
    
    void RulerProgress::drawRulerTop(QPainter *painter)
    {
        painter->save();
        painter->setPen(lineColor);
    
        double initX = 0;
    
        //绘制横向标尺上部分底部线
        double initTopY = 0;
        QPointF lineTopLeftPot = QPointF(initX, initTopY);
        QPointF lineTopRightPot = QPointF(width() - initX, initTopY);
        painter->drawLine(lineTopLeftPot, lineTopRightPot);
    
        //绘制上部分及下部分横向标尺刻度
        double length = width();
        //计算每一格移动多少
        double increment = length / (maxValue - minValue);
        //长线条短线条长度
        int longLineLen = 15;
        int shortLineLen = 10;
    
        //根据范围值绘制刻度值及刻度值 长线条需要移动10像素 短线条需要移动5像素
        for (int i = minValue; i <= maxValue; i = i + shortStep) {
            if (i % longStep == 0) {
                QPointF topPot = QPointF(initX, initTopY);
                QPointF bottomPot = QPointF(initX, initTopY + longLineLen);
                painter->drawLine(topPot, bottomPot);
    
                //第一个值和最后一个值不要绘制
                if (i == minValue || i == maxValue) {
                    initX += increment * shortStep;
                    continue;
                }
    
                QString strValue = QString("%1").arg((double)i, 0, 'f', precision);
                double textWidth = fontMetrics().width(strValue);
                double textHeight = fontMetrics().height();
    
                QPointF textPot = QPointF(initX - textWidth / 2, initTopY + textHeight + longLineLen);
                painter->drawText(textPot, strValue);
            } else {
                if (i % (longStep / 2) == 0) {
                    shortLineLen = 10;
                } else {
                    shortLineLen = 6;
                }
    
                QPointF topPot = QPointF(initX, initTopY);
                QPointF bottomPot = QPointF(initX, initTopY + shortLineLen);
                painter->drawLine(topPot, bottomPot);
            }
    
            initX += increment * shortStep;
        }
    
        painter->restore();
    }
    
    void RulerProgress::drawRulerBottom(QPainter *painter)
    {
        painter->save();
        painter->setPen(lineColor);
    
        double initX = 0;
    
        //绘制横向标尺下部分底部线
        double initBottomY = height();
        QPointF lineBottomLeftPot = QPointF(initX, initBottomY);
        QPointF lineBottomRightPot = QPointF(width() - initX, initBottomY);
        painter->drawLine(lineBottomLeftPot, lineBottomRightPot);
    
        //绘制上部分及下部分横向标尺刻度
        double length = width();
        //计算每一格移动多少
        double increment = length / (maxValue - minValue);
        //长线条短线条长度
        int longLineLen = 15;
        int shortLineLen = 10;
    
        //根据范围值绘制刻度值及刻度值 长线条需要移动10像素 短线条需要移动5像素
        for (int i = minValue; i <= maxValue; i = i + shortStep) {
            if (i % longStep == 0) {
                QPointF topPot = QPointF(initX, initBottomY);
                QPointF bottomPot = QPointF(initX, initBottomY - longLineLen);
                painter->drawLine(topPot, bottomPot);
    
                //第一个值和最后一个值不要绘制
                if (i == minValue || i == maxValue) {
                    initX += increment * shortStep;
                    continue;
                }
    
                QString strValue = QString("%1").arg((double)i, 0, 'f', precision);
                double textWidth = fontMetrics().width(strValue);
                double textHeight = fontMetrics().height();
    
                QPointF textPot = QPointF(initX - textWidth / 2, initBottomY - textHeight / 2 - longLineLen);
                painter->drawText(textPot, strValue);
            } else {
                if (i % (longStep / 2) == 0) {
                    shortLineLen = 10;
                } else {
                    shortLineLen = 6;
                }
    
                QPointF topPot = QPointF(initX, initBottomY);
                QPointF bottomPot = QPointF(initX, initBottomY - shortLineLen);
                painter->drawLine(topPot, bottomPot);
            }
    
            initX += increment * shortStep;
        }
    
        painter->restore();
    }
    

    控件介绍

    1. 超过130个精美控件,涵盖了各种仪表盘、进度条、进度球、指南针、曲线图、标尺、温度计、导航条、导航栏,flatui、高亮按钮、滑动选择器、农历等。远超qwt集成的控件数量。
    2. 每个类都可以独立成一个单独的控件,零耦合,每个控件一个头文件和一个实现文件,不依赖其他文件,方便单个控件以源码形式集成到项目中,较少代码量。qwt的控件类环环相扣,高度耦合,想要使用其中一个控件,必须包含所有的代码。
    3. 全部纯Qt编写,QWidget+QPainter绘制,支持Qt4.6到Qt5.12的任何Qt版本,支持mingw、msvc、gcc等编译器,不乱码,可直接集成到Qt Creator中,和自带的控件一样使用,大部分效果只要设置几个属性即可,极为方便。
    4. 每个控件都有一个对应的单独的包含该控件源码的DEMO,方便参考使用。同时还提供一个所有控件使用的集成的DEMO。
    5. 每个控件的源代码都有详细中文注释,都按照统一设计规范编写,方便学习自定义控件的编写。
    6. 每个控件默认配色和demo对应的配色都非常精美。
    7. 超过120个可见控件,6个不可见控件。
    8. 部分控件提供多种样式风格选择,多种指示器样式选择。
    9. 所有控件自适应窗体拉伸变化。
    10. 集成自定义控件属性设计器,支持拖曳设计,所见即所得,支持导入导出xml格式。
    11. 自带activex控件demo,所有控件可以直接运行在ie浏览器中。
    12. 集成fontawesome图形字体+阿里巴巴iconfont收藏的几百个图形字体,享受图形字体带来的乐趣。
    13. 所有控件最后生成一个dll动态库文件,可以直接集成到qtcreator中拖曳设计使用。

    SDK下载


  • 相关阅读:
    快速理解平衡二叉树、B-tree、B+tree、B*tree
    centos 7(6) linux系统安装 mysql5.7.17(glibc版)
    关于使用Hibernate+spring+dubbo的实现微服务对象查询
    Keepalived+Nginx实现高可用(HA)
    Nginx源码安装
    Keepalived安装与配置
    单点fastDfs+centos7搭建
    Dubbo+zookeeper使用方法以及注意事项
    mac 下 iterm2 不能使用 rz sz
    java 无符号整型
  • 原文地址:https://www.cnblogs.com/feiyangqingyun/p/10741698.html
Copyright © 2011-2022 走看看