时间:2023-01-01 12:42:29 | 栏目:C代码 | 点击:次
我们在很多的安装包中,在安装过程中,经常可以在底部看到一个漂亮的进度条,上面悬浮着显示对应的进度,然后底部进度多种颜色渐变展示,Qt自带的进度条或者操作系统的进度条样式,不够炫,这次索性直接来个自定义绘制实现,至于是继承QWidget类还是QProgressBar类,都无所谓,如果是继承自QWidget类的话,就需要自己设置最大值最小值范围值,而继承自QProgressBar的话就可以直接用自带的函数来实现。这次编写的进度条,提示信息的背景色也是和下面的进度颜色完全一致跟着变化的。
1:可设置最小值/最大值/范围值/当前值
2:可设置是否百分比显示
3:可设置边距,流出空隙防止提示信息跑到外面
4:可设置进度的颜色,可以是渐变画刷
5:可设置背景颜色/文字颜色/提示背景
6:可设置圆角角度
7:如果设置了进度画刷则提示背景也采用该画刷
#ifndef PROGRESSTIP_H #define PROGRESSTIP_H /** * 提示进度条控件 作者:feiyangqingyun(QQ:517216493) 2019-10-05 * 1:可设置最小值/最大值/范围值/当前值 * 2:可设置是否百分比显示 * 3:可设置边距,流出空隙防止提示信息跑到外面 * 4:可设置进度的颜色,可以是渐变画刷 * 5:可设置背景颜色/文字颜色/提示背景 * 6:可设置圆角角度 * 7:如果设置了进度画刷则提示背景也采用该画刷 */ #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 ProgressTip : public QWidget #else class ProgressTip : 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(bool percent READ getPercent WRITE setPercent) Q_PROPERTY(int padding READ getPadding WRITE setPadding) Q_PROPERTY(int radius READ getRadius WRITE setRadius) Q_PROPERTY(QBrush valueBrush READ getValueBrush WRITE setValueBrush) Q_PROPERTY(QColor valueColor READ getValueColor WRITE setValueColor) Q_PROPERTY(QColor bgColor READ getBgColor WRITE setBgColor) Q_PROPERTY(QColor tipColor READ getTipColor WRITE setTipColor) Q_PROPERTY(QColor textColor READ getTextColor WRITE setTextColor) public: explicit ProgressTip(QWidget *parent = 0); protected: void paintEvent(QPaintEvent *); void drawBg(QPainter *painter); void drawTip(QPainter *painter); void drawValue(QPainter *painter); private: double minValue; //最小值 double maxValue; //最大值 double value; //目标值 bool percent; //百分比显示 int padding; //边距 int radius; //圆角角度 QBrush valueBrush; //进度画刷 QColor valueColor; //进度颜色 QColor bgColor; //背景颜色 QColor tipColor; //提示背景颜色 QColor textColor; //文字颜色 public: double getMinValue() const; double getMaxValue() const; double getValue() const; bool getPercent() const; int getPadding() const; int getRadius() const; QBrush getValueBrush() const; QColor getValueColor() const; QColor getBgColor() const; QColor getTipColor() const; QColor getTextColor() 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 setPercent(bool percent); //设置边距 void setPadding(int padding); //设置圆角角度 void setRadius(int radius); //设置进度画刷+进度颜色+背景颜色+提示背景+文字颜色 void setValueBrush(const QBrush &valueBrush); void setValueColor(const QColor &valueColor); void setBgColor(const QColor &bgColor); void setTipColor(const QColor &tipColor); void setTextColor(const QColor &textColor); Q_SIGNALS: void valueChanged(double value); }; #endif // PROGRESSPLAY_H
void ProgressTip::paintEvent(QPaintEvent *) { //绘制准备工作,启用反锯齿,平移坐标轴中心,等比例缩放 QPainter painter(this); painter.setRenderHints(QPainter::Antialiasing | QPainter::TextAntialiasing); //绘制背景 drawBg(&painter); //绘制上部分提示信息 drawTip(&painter); //绘制进度 drawValue(&painter); } void ProgressTip::drawBg(QPainter *painter) { painter->save(); painter->setPen(Qt::NoPen); painter->setBrush(bgColor); QRect rect(padding, (height() / 3) * 2, width() - padding * 2, height() / 3); painter->drawRoundedRect(rect, radius, radius); painter->restore(); } void ProgressTip::drawTip(QPainter *painter) { painter->save(); painter->setPen(Qt::NoPen); painter->setBrush(valueBrush == Qt::NoBrush ? tipColor : valueBrush); //计算当前值对应的百分比 double step = value / (maxValue - minValue); int progress = (width() - padding * 2) * step; //绘制上部分提示信息背景 QRect rect(progress, 0, padding * 2, (double)height() / 2.1); painter->drawRoundedRect(rect, 2, 2); //绘制倒三角 int centerX = rect.center().x(); int initY = rect.height(); int offset = 5; QPolygon pts; pts.append(QPoint(centerX - offset, initY)); pts.append(QPoint(centerX + offset, initY)); pts.append(QPoint(centerX, initY + offset)); painter->drawPolygon(pts); //绘制文字 QString strValue; if (percent) { double temp = value / (maxValue - minValue) * 100; strValue = QString("%1%").arg(temp, 0, 'f', 0); } else { strValue = QString("%1").arg((double)value, 0, 'f', 0); } painter->setPen(textColor); painter->drawText(rect, Qt::AlignCenter, strValue); painter->restore(); } void ProgressTip::drawValue(QPainter *painter) { painter->save(); painter->setPen(Qt::NoPen); //定义了画刷则取画刷,可以形成渐变效果 painter->setBrush(valueBrush == Qt::NoBrush ? valueColor : valueBrush); //计算当前值对应的百分比 double step = value / (maxValue - minValue); int progress = (width() - padding * 2) * step; QRect rect(padding, (height() / 3) * 2, progress, height() / 3); painter->drawRoundedRect(rect, radius, radius); painter->restore(); }