2016-05-16 75 views
0

我期待有一個自定義進度條,其進度通過自定義動畫更改。我將有相當多的這個小部件的實例,它們都應該平穩快速地運行。提高性能自定義進度條動畫

我的第一次嘗試是使用常規的QProgressBar,使用樣式表對其進行自定義,然後使用QPropertyAnimation來動態更改狀態。

這工作正常,但速度非常慢。比方說,我開始動畫時的數值爲0%,上升到50%,並希望在500毫秒的時間內完成。它根本不平坦,但有三個明確區分的步驟。如果我放棄樣式表,它將工作順利。

回答

1

那麼,似乎工作正常使用派生類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(); 

仍然開放反饋或更好的解決方案!

+2

這只是一個小小的改進,但是緩存你的顏色和'QLinearGradient'會稍微加快'paintEvent'實現的速度。 –