赞
踩

绘制过程大致分为5步:
1.绘制整个页面背景(如果要用在其他窗体上则忽略此步骤,并设置背景透明)。
2.绘制文字(“占用率”)和百分比。
3.绘制2个上下对称的梯形(进度条的两边部分)以及若干个平行四边形(进度条的中间部分),颜色为黑色背景。
4.绘制进度条的渐变边框。
5.根据具体占用率计算应该填充的进度,注意填充的块可能是三角形,梯形,五边形,平行四边形,需要根据进度判断;
class ProgressBar : public QWidget { Q_OBJECT public: explicit ProgressBar(QWidget *parent = 0); protected: void paintEvent(QPaintEvent *); void drawPolygonBackground(QPointF topLeft, QPointF bottomLeft, QPointF bottomRight, QPointF topRight, QPainter *painter); void drawPolygonShadowground( QPointF topLeft, QPointF bottomLeft, QPointF bottomRight, QPointF topRight, QPainter *painter); void drawPolygonUseground(QPolygonF polygon, QPainter *painter); private: QString backgroundColor = "#ffe228"; int polygonNum = 10; //多边形数 double polygonHeightScale = 0.25; double value = 0.58; //概率 QString title = QStringLiteral("占用率"); int space = 4; };
void ProgressBar::paintEvent(QPaintEvent *) { //绘制准备工作,启用反锯齿 QPainter painter(this); //绘制背景 QPainterPath path; path.addRect(QRectF(rect())); painter.setOpacity(1); painter.fillPath(path, QColor(4,44,54)); //绘制文字 "占用率" painter.setPen(QColor("#ffffff")); int fontSize = this->height() * (1 - polygonHeightScale) - space*2; QFont fontTitle("Source Han Sans CN", fontSize, QFont::Bold, false); painter.setFont(fontTitle); QPointF textPot(0,fontSize + space); painter.drawText(textPot, title); //绘制进度百分比文字 painter.setPen(QColor("#ffe228")); QString percent = QString::number(value*100); QPointF percentPot(this->width() - fontSize *3 ,fontSize + space); painter.drawText(percentPot, QString("%1%").arg(percent)); double polygonWidth = (double)this->width() / (1.5*polygonNum -1); int useWidth = this->width() * value; for(int i = 0; i < polygonNum; i++) { int topStartX = polygonWidth*1.5*i; int topEndX = polygonWidth + polygonWidth*1.5*i; int bottomStartX; int bottomEndX; if(i == 0) { bottomStartX = 0; bottomEndX = polygonWidth*0.5; } else { bottomStartX = polygonWidth*1.5*i - polygonWidth*0.5; bottomEndX = bottomStartX + polygonWidth; } QPointF topLeft(topStartX, (1-polygonHeightScale)*this->height()); QPointF bottomLeft(bottomStartX, this->height()); QPointF bottomRight(bottomEndX, this->height()); QPointF topRight(topEndX, (1-polygonHeightScale)*this->height()); //画四边形内部背景 drawPolygonBackground(topLeft, bottomLeft, bottomRight, topRight, &painter); //画四边形边框渐变背景 drawPolygonShadowground(topLeft, bottomLeft, bottomRight, topRight, &painter); //画占用进度 if(bottomStartX < useWidth) { //三角形 if(topStartX > useWidth) { topLeft.setX(useWidth); double currentHeight = (useWidth - bottomStartX)* polygonHeightScale*this->height() /(topStartX -bottomStartX) ; topLeft.setY(this->height() - currentHeight); bottomRight.setX(useWidth); QPolygonF pf; pf << topLeft << bottomLeft << bottomRight; drawPolygonUseground(pf, &painter); } else if (topStartX < useWidth && bottomEndX > useWidth) { topRight.setX(useWidth); bottomRight.setX(useWidth); QPolygonF pf; pf << topLeft << bottomLeft << bottomRight << topRight; drawPolygonUseground(pf, &painter); } else if (bottomEndX < useWidth && useWidth < topEndX) { double currentHeight = (topEndX - useWidth)* polygonHeightScale*this->height() /(topEndX -bottomEndX) ; QPointF pTopRight(useWidth, (1-polygonHeightScale)*this->height()); QPointF pBottomRight(useWidth, (1-polygonHeightScale)*this->height() + currentHeight); QPolygonF pf; pf << topLeft << bottomLeft << bottomRight << pBottomRight << pTopRight; drawPolygonUseground(pf, &painter); } else { QPolygonF pf; pf << topLeft << bottomLeft << bottomRight << topRight; drawPolygonUseground(pf, &painter); } } } } void ProgressBar::drawPolygonBackground(QPointF topLeft, QPointF bottomLeft, QPointF bottomRight, QPointF topRight, QPainter *painter) { painter->save(); painter->setRenderHint(QPainter::Antialiasing, true); QColor color("#000000"); painter->setBrush(color); QPolygonF polygon; polygon << topLeft << bottomLeft << bottomRight << topRight; painter->drawPolygon(polygon); painter->restore(); } void ProgressBar::drawPolygonShadowground(QPointF topLeft, QPointF bottomLeft, QPointF bottomRight, QPointF topRight, QPainter *painter) { painter->save(); painter->setRenderHint(QPainter::Antialiasing, true); int x1 = topLeft.x(), y1 = topLeft.y(); int x2 = bottomLeft.x(), y2 = bottomLeft.y(); int x3 = bottomRight.x(), y3 = bottomRight.y(); int x4 = topRight.x(), y4 = topRight.y(); QColor color(backgroundColor); painter->setBrush(Qt::NoBrush); for (int i = 0; i < 10; i++) { color.setAlpha(120 - qSqrt(i) * 60); painter->setPen(color); QPointF pTopLeft; QPointF pBottomLeft; QPointF pBottomRight; QPointF pTopRight; pTopLeft.setX(x1 + i); pTopLeft.setY(y1 + i); pBottomLeft.setX(x2 + i); pBottomLeft.setY(y2 - i); pBottomRight.setX(x3 - i); pBottomRight.setY(y3 - i); pTopRight.setX(x4 - 2*i); pTopRight.setY(y4 + i); QPolygonF polygon; polygon << pTopLeft << pBottomLeft << pBottomRight << pTopRight; painter->drawPolygon(polygon); } painter->restore(); } void ProgressBar::drawPolygonUseground(QPolygonF polygon, QPainter *painter) { painter->save(); painter->setRenderHint(QPainter::Antialiasing, true); QColor color(backgroundColor); painter->setBrush(color); painter->drawPolygon(polygon); painter->restore(); }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。