问题描述
有没有办法改变一个QPushButton的背景颜色,通过修改它的QPalette对象?我意识到它可以做到与样式表,但想要一个方法来通过修改QPalette。类似于在未按下按钮时更改按钮的背景颜色:
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);
推荐答案
连接到QPushButton的信号,并使用所需的颜色更新调色板,然后连接到信号,并将颜色切换回旧颜色。
Connect to QPushButton's pressed() signal and update the palette with your desired color and then connect to released() signal and switch back the color to the old color. That should do it.
您可能想要运行设置颜色后或在极端情况下。
You may want to run update() after settings the colors or in extreme cases repaint().
这篇关于按下时改变QPushButton颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!