void BarChartWidget::drawBars(QPainter &painter) { std::vector<int> sizes(BAR_COUNT, 0); fillSizes(sizes); QRect baseRectangle = rect(); int activeWidth = (int) std::floor( baseRectangle.width() * (1 - 2 * PADDING) ); int singleBarWidth = (int) std::floor((activeWidth - (BAR_COUNT + 1) * SPACING) / BAR_COUNT); int baseLineY = (int) std::floor(baseRectangle.bottom() - PADDING_BOTTOM); int currX = (int) std::floor(baseRectangle.left() + baseRectangle.width() * PADDING); currX += SPACING; for (int i = 0; i < BAR_COUNT; i++) { QPoint pointTopLeft(currX, baseLineY - sizes.at((unsigned long) i)); QPoint pointBottomRight(currX + singleBarWidth, baseLineY - 1); QRect bar(pointTopLeft, pointBottomRight); painter.setPen(mainColor); painter.drawRect(bar); painter.fillRect(bar, barColor); QPoint textTopLeft(currX, baseLineY); QPoint textBottomRight(currX + singleBarWidth, baseRectangle.bottom()); QRect textRect(textTopLeft, textBottomRight); painter.setPen(mainColor); painter.drawText(textRect, Qt::AlignCenter, labels->at((unsigned long) i)); QPoint valueTopLeft(currX, baseLineY - sizes.at((unsigned long) i) - VALUE_LABEL_HEIGHT); QPoint valueBottomRight(currX + singleBarWidth, baseLineY); QRect valueLabelRect(valueTopLeft, valueBottomRight); QString textValue = QString::number(values->at((unsigned long) i)); painter.drawText(valueLabelRect, Qt::AlignHCenter | Qt::AlignTop, textValue); currX += singleBarWidth + SPACING; } }
void ProgressBarColor::drawBar(QPainter *painter) { painter->save(); //自动计算文字字体大小 QFont f(font()); f.setPixelSize((width() / 10) * 0.35); painter->setFont(f); //计算进度值字符的宽度 double currentValue = (double)(value - minValue) * 100 / (maxValue - minValue); QString strValue = QString("当前值:%1%").arg(currentValue, 0, 'f', precision); QString strMaxValue = QString("%1%").arg(maxValue, 0, 'f', precision); //字符的宽度取最大值字符的宽度 + 10 int textWidth = painter->fontMetrics().width(strMaxValue) + 10; //绘制进度值背景 QPointF textTopLeft(width() - space - textWidth, space); QPointF textBottomRight(width() - space, height() - space); QRectF textRect(textTopLeft, textBottomRight); painter->setPen(barBgColor); painter->setBrush(barBgColor); painter->drawRoundedRect(textRect, radius, radius); //绘制进度值 painter->setPen(textColor); painter->drawText(textRect, Qt::AlignCenter, strValue); //绘制进度条背景 QRectF barBgRect(QPointF(space, space), QPointF(width() - space * 2 - textWidth, height() - space)); painter->setPen(Qt::NoPen); painter->setBrush(barBgColor); painter->drawRoundedRect(barBgRect, radius, radius); //绘制进度条 double length = width() - space - space * 2 - textWidth; //计算每一格移动多少 double increment = length / (maxValue - minValue); QRectF barRect(QPointF(space, space), QPointF(space + increment * (value - minValue), height() - space)); painter->setBrush(barColor); painter->drawRoundedRect(barRect, radius, radius); //绘制背景分割线条 每一格长度7 painter->setPen(lineColor); int initX = 5; int lineCount = barBgRect.width() / step; double lineX = (double)barBgRect.width() / lineCount; //线条高度在进度条高度上 - 1 while (lineCount > 0) { QPointF topPot(initX + lineX, space + 1); QPointF bottomPot(initX + lineX, height() - space - 1); painter->drawLine(topPot, bottomPot); initX += lineX; lineCount--; } painter->restore(); }