咱們在不少的安裝包中,在安裝過程當中,常常能夠在底部看到一個漂亮的進度條,上面懸浮着顯示對應的進度,而後底部進度多種顏色漸變展現,Qt自帶的進度條或者操做系統的進度條樣式,不夠炫,此次索性直接來個自定義繪製實現,至因而繼承QWidget類仍是QProgressBar類,都無所謂,若是是繼承自QWidget類的話,就須要本身設置最大值最小值範圍值,而繼承自QProgressBar的話就能夠直接用自帶的函數來實現。此次編寫的進度條,提示信息的背景色也是和下面的進度顏色徹底一致跟着變化的。linux
#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(); }