那麼,似乎工作正常使用派生類QProgressBar的,它比使用樣式錶快得多,雖然要自定義調整寬度和高度:
void ColorBar::paintEvent(QPaintEvent *pe)
{
QRect region = pe->rect();
QPainter painter(this);
QColor borderColor;
borderColor.setNamedColor("#a0a0a0");
QColor lightColor = QColor(255, 255, 255);
QColor darkColor = QColor(225, 225, 225);
int barHeight = static_cast<int>(height() * 1./4. + 0.5);
QRect drawRect(0, static_cast<int>(height()/2. - barHeight/2. + 0.5), width() * .9 * value()/maximum(), barHeight);
QLinearGradient g(drawRect.topLeft(), drawRect.bottomLeft());
g.setColorAt(0., lightColor);
g.setColorAt(1., darkColor);
painter.setPen(QPen(borderColor));
painter.setBrush(QBrush(g));
painter.drawRect(drawRect);
}
動畫這個酒吧是然後直接和快速:
QPropertyAnimation* x = new QPropertyAnimation(percentageBar, "value");
x->setStartValue(percentageBar->value());
x->setEndValue(newValue);
x->setDuration(500);
x->start();
仍然開放反饋或更好的解決方案!
這只是一個小小的改進,但是緩存你的顏色和'QLinearGradient'會稍微加快'paintEvent'實現的速度。 –