Specifying Color for progress bar in QProgressBar
-
Hello all,
I am trying to find a way to change the color of the progress bar in QProgressBar. I tried this code snippet here:
QPalette p = this->palette(); p.setColor(QPalette::Highlight, QColor(Qt::green)); this->setPalette(p);
However, this did not work.
I would need to change the colors dynamically so I wouldn't like to use stylesheets. I feel that I need to override the paint function but I am not sure.
I will consider stylesheets as a last resort.
-
@Omni_Philm said in Specifying Color for progress bar in QProgressBar:
I would need to change the colors dynamically so I wouldn't like to use stylesheets
Stylesheet can be dynamic too.
QColor barColor = QColor(255, 0, 0); QString style = QString("QProgressBar::chunk { background-color: %1; width: 20px; }").arg(barColor.name()); this->setStylesheet(style);
https://doc.qt.io/qt-5/stylesheet-examples.html#customizing-qprogressbar
-
If you override the paint function (paintEvent), then you'll need to paint the whole progress bar yourself. You don't want to do that...
You know, sometimes the default QStyle don't use QPalette to get the color.
In this situation, stylesheet is your best choice. Or you may consider changing to fusion style, which is not very "native" though. -
@Pl45m4 said in Specifying Color for progress bar in QProgressBar:
@Omni_Philm said in Specifying Color for progress bar in QProgressBar:
I would need to change the colors dynamically so I wouldn't like to use stylesheets
Stylesheet can be dynamic too.
QColor barColor = QColor(255, 0, 0); QString style = QString("QProgressBar::chunk { background-color: %1; width: 20px; }").arg(barColor.name()); this->setStylesheet(style);
https://doc.qt.io/qt-5/stylesheet-examples.html#customizing-qprogressbar
Thanks however, each time I get into using stylesheets, there are always additional issues that pop up and I have to reformat the look of everything.
I liked the the way the progress bar looked with the rounded corners. Now it is just a rectangle.
Edit:
Nevermind, I got it working!Edit 2:
Ok, I got it mostly working. I am trying to change the width of the chunk and it seems that width: 20px is breaking it