PDA

View Full Version : troubles with changing progressBar color



dilidpan
28th April 2009, 15:36
Hello to everybody,

I'm using the latest version of Qt Creator.
I have problems while trying to change the progressBar color. I tried-out the examples in tutorials and all suggestions in forums. It still don't work at all.
I tried this suggestion.

QPalette pal = ui->progressBar->palette();
pal.setColor(QPalette::Highlight, QColor("blue"));
pal.setColor(QPalette::Foreground, QColor("magenta"));
pal.setColor(QPalette::Background, QColor("yellow"));
pal.setColor(QPalette::Button, QColor("red"));
ui->progressBar->setPalette(pal);

What happens is that only the text color of the percentage is changing.

:confused: When I have concrete value I must change the color of progress bar to red for example.
Can someone help me to solve this problem. I don't understand why this code doesn't work :confused: .

spirit
28th April 2009, 15:39
works fine. have a look at style sheet example (http://doc.trolltech.com/4.5/stylesheet-examples.html#customizing-qprogressbar).

dilidpan
28th April 2009, 15:45
But it doesn't work at my application. It only sets the percentage text color. The other colors are not changed. I want to set the bar to be red, but it's still green. Where am I wrong?

spirit
28th April 2009, 15:46
open the Qt Designer and play with a progress bar palette.

spirit
28th April 2009, 15:49
ah, add this line


pal.setColor(QPalette::Window, QColor("red"));

dilidpan
28th April 2009, 15:49
OK. I'll try.

dilidpan
28th April 2009, 15:51
Still doesn't work.

Lykurg
28th April 2009, 16:07
Try this on your machine:

#include <QtGui>

int main(int argc, char *argv[])
{
QApplication a(argc, argv);
QProgressBar bar;
bar.setValue(50);
QPalette pal = bar.palette();
pal.setColor(QPalette::Highlight, QColor("yellow"));
bar.setPalette(pal);
bar.show();
return a.exec();
}

If it is yellow you probably set a style sheet in ui->progressBar. Because then the palette quit to work.

dilidpan
28th April 2009, 16:25
The progressBar is green. No change at all.

Lykurg
28th April 2009, 17:24
Ok, which style and Qt version do you use, because the sample works well at my system?

dilidpan
29th April 2009, 10:26
I am programming on Windows XP and use Qt Creator 1.0.0 Based on Qt 4.5.0.

dilidpan
29th April 2009, 11:03
I did it :). It works with suggestion from the following link: http://www.qtcentre.org/forum/f-qt-programming-2/t-problem-in-qprogressbar-setstylesheet-20557.html
from this forum -> problem in QProgressBar setStyleSheet.

Lykurg
29th April 2009, 11:12
I did it :). It works with suggestion from the following link: http://www.qtcentre.org/forum/f-qt-programming-2/t-problem-in-qprogressbar-setstylesheet-20557.html
from this forum -> problem in QProgressBar setStyleSheet.
:confused: They work with style sheets not with palette? Which suggestion do you refer to?

momchil
15th September 2012, 03:58
They do it by setting the style sheet, similar to this:

ui_lcd.progressBar->setStyleSheet("QProgressBar::chunk {background: qlineargradient(x1: 0, y1: 0.5, x2: 1, y2: 0.5, stop: 0 red, stop: 1 white); }");

ui_lcd.progressBar->update();
ui_lcd.progressBar->show();

that does what I needed... :cool:
thanks to everyone involved!

P.S. note what that post warns about -- have to set the style sheet in a *single* setStyleSheet() call...