進度條標尺控件的應用場景通常是須要手動拉動進度,上面有標尺能夠看到當前進度,相似於qslider控件,其實就是qslider+qprogressbar的雜交版本,不過我才用的是純qpainter繪製的方式,這樣很是靈活可靠,繼承自qwidget,這個控件屬於標尺類控件中的一個,就是在刻度尺控件基礎上增長了鼠標按下拖動進度的功能。瀏覽器
#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(); }