View Full Version : QPushButton color when clicked

24th April 2008, 22:12
I have a push button that I want to always look red even when I have done setChecked(true) to keep it depressed.

I have this:

m_fourButton->setStyleSheet(QString("QPushButton {background-color: red;} QPushButton:checked{background-color: red;} QPushButton:pressed {background-color: red;}"));

I also tried background:red.

I started with QPalellete which gives me the same results.

m_redPalette.setColor(QPalette::Button, Qt::red);
QBrush brush;
m_redPalette.setBrush(QPalette::Window, brush);

and tried everything that Palette would let me for Color Roles.

The result is that it is red when it is not checked but only the border is red when it is checked (depressed). Is there anything else I can do to make the pressed button red?

I've attached a zip file with an image of the buttons (it wouldn't let me upload my .bmp file).


25th April 2008, 10:43
I can't reproduce your problem. If I use this code:

button = new QPushButton(tr("Red Button"));
button->setStyleSheet(QString("QPushButton {background-color: red;}"));

I get red a button also red when depressed. Your setStyleSheet code also works without a problem. I just use the default GUI style, if you use a custom GUI style maybe you should look for the problem there.

25th April 2008, 18:32
That's really strange because I created a new project and tried it again and I still have it. What version are you using? I am using 4.3.1 on windows.

I am not explicitly setting any kind of GUI style.

I tried both with a designer made button and in-code button:

ui.pushButton->setStyleSheet(QString("QPushButton {background-color: red;} QPushButton:checked{background-color: red;} QPushButton:pressed {background-color: red;}"));

QPushButton* button = new QPushButton(tr("Red Button"), this);
button->setStyleSheet(QString("QPushButton {background-color: red;} QPushButton:checked{background-color: red;} QPushButton:pressed {background-color: red;}"));

Thanks for looking at it.

30th April 2008, 19:34
Turns out it is a regression starting with version 4.3.0 and Trolltech has now entered it into their tracking system. It works in 4.2.3.

A side note, they say that the palette approach will not work on Windows XP because XP's theme engine, not Qt, controls the drawing of the buttons. Using style sheets is the correct approach.