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)
{
pal.
setColor(QPalette::Button, Qt
::green);
button->setAutoFillBackground(true);
button->setPalette(pal);
button->update();
}
}
OnButtonToggled(bool bChecked)
{
if(bChecked)
{
QPalette pal = button->palette();
pal.setColor(QPalette::Button, Qt::green);
button->setAutoFillBackground(true);
button->setPalette(pal);
button->update();
}
}
To copy to clipboard, switch view to plain text mode
All this does is give me a green border around the button.
Palette.jpg
I also tried changing the brush of QPallete::Button
OnButtonToggled(bool bChecked)
{
if(bChecked)
{
button->setAutoFillBackground(true);
button->setPalette(pal);
button->update();
}
}
OnButtonToggled(bool bChecked)
{
if(bChecked)
{
QPalette pal = button->palette();
pal.setBrush(QPalette::Button, QBrush(Qt::green));
button->setAutoFillBackground(true);
button->setPalette(pal);
button->update();
}
}
To copy to clipboard, switch view to plain text mode
but I get the same thing.
if I change the style sheet
button->setStyleSheet("background-color:green;");
button->setStyleSheet("background-color:green;");
To copy to clipboard, switch view to plain text mode
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.
StyleSheet.jpg
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.
Bookmarks