Search code examples
c++qtbackground-colorqprogressbar

How to change color of default style for progressbar in Qt


How I can change green tint in default QProgressbar style, without changing other default gradients and effects (a little noticeable "flow white chunk" effect):

Default QProgressbar style

.

I was tried to set new combination of background colors for QProgressBar::chunk:horizontal using qlineargradient, but I did not succeed to keep mentioned effect with any of such stylesheets.


Solution

  • Possibly try to update StyleSheet with timer like this:

    mRunner = 0.1;
    
    QTimer *mTimer = new QTimer(this);
    connect(mTimer, SIGNAL(timeout()), this, SLOT(updateProgress()));
    mTimer->start(40);
    

    and method should change gradient for each new step:

    void MainWindow::updateProgress()
    {
        QString lStyle = QString("QProgressBar::chunk {background-color: qlineargradient(x1:0, y1:0, x2:1, y2:1, stop:0 #b4b4b4, stop:%1 white, stop:1 #b4b4b4);}").arg(mRunner);
        ui->progressBar->setStyleSheet(lStyle);
    
        mRunner += 0.01;
        if (mRunner > 1) {
            mRunner = 0.1;
        }
    }