PDA

View Full Version : change color QPushButton Previous post methods not working Qt 5.4.1 VS 2013 Windows 7



TonyInSoMD
2nd May 2017, 12:21
I've looked at all the previous posts on how to change the color of a button that I could search and find, but they don't seem to work. I'm using Qt 5.4.1 VS 2013 and Windows 7. I'm trying to change the color of a button to green when checked. I've tried changing the color of QPalette::Button


OnButtonToggled(bool bChecked)
{
if(bChecked)
{
QPalette pal = button->palette();
pal.setColor(QPalette::Button, Qt::green);
button->setAutoFillBackground(true);
button->setPalette(pal);
button->update();
}
}

All this does is give me a green border around the button.
12451

I also tried changing the brush of QPallete::Button


OnButtonToggled(bool bChecked)
{
if(bChecked)
{
QPalette pal = button->palette();
pal.setBrush(QPalette::Button, QBrush(Qt::green));
button->setAutoFillBackground(true);
button->setPalette(pal);
button->update();
}
}

but I get the same thing.

if I change the style sheet


button->setStyleSheet("background-color:green;");

it changes to a squared off button instead of the rounded button it used to be, which means it no longer matches all the other buttons in the window.
12452
To be honest, I don't understand why the palette option doesn't work. In all the previous posts I've read, that's what people did. If I remember right, I needed to change the color of a button a couple of years ago, and the palette method is how I did it. I was using Qt 4.3.1 and Windows XP at that time though.

d_stranz
2nd May 2017, 23:28
You might be running into this (from the Qt Style Sheets Reference):



QPushButton
Supports the box model. Supports the :default, :flat, :checked pseudo states.
For QPushButton with a menu, the menu indicator is styled using the ::menu-indicator subcontrol. Appearance of checkable push buttons can be customized using the : open and : closed pseudo-states.
Warning: If you only set a background-color on a QPushButton, the background may not appear unless you set the border property to some value. This is because, by default, the QPushButton draws a native border which completely overlaps the background-color. For example,

QPushButton { background-color: red; border: none; }

See Customizing QPushButton for an example.


So try setting the border as well.

TonyInSoMD
8th May 2017, 20:00
My problem was I had no clue on how to set up style sheets. In the help I stumbled across "Qt Style Sheets Reference" which told me the syntax I needed and what it did, like "border-radius" to round the corners. I didn't know that border-radius existed or what it did. I didn't know what to call anything so that I could set it. I found it there. It's a great reference, you just have to find it. I didn't think finding it was all that easy.

TonyInSoMD
10th May 2017, 12:11
My problem was I had no clue on how to set up style sheets. In the help I stumbled across "Qt Style Sheets Reference" which told me the syntax I needed and what it did, like "border-radius" to round the corners. I didn't know that border-radius existed or what it did. I didn't know what to call anything so that I could set it. I found it there. It's a great reference, you just have to find it. I didn't think finding it was all that easy.

BTW, you have to scroll down the page some to get to the list of properties