C++ – Changing QPushButton Color when Pressed

cqtuser interface

Is there a way to change the background color of a QPushButton which is pressed by modifying its QPalette object? I realize it can be done with style sheets but would like a way to do it by modifying the QPalette. Something similar to how one changes the background color of the button when not pressed:

QPushButton myButton;
QPalette p(myButton.palette());

p.setColor(QPalette::Button, QColor("#ffffff"));
myButton.setPalette(p);

Best Answer

Simply add a stylesheet to the qbushbutton itself or to his parent qwidget:

qwidget.setStyleSheet("QPushButton:checked { background-color: red; }")

This will set the background color to red when the QPushButton is checked.