进度条标尺控件的应用场景一般是需要手动拉动进度,上面有标尺可以看到当前进度,类似于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
#ifdef quc
#if (QT_VERSION < QT_VERSION_CHECK(5,7,0))
#include
#else
#include
#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();
}