按下时改变QPushButton颜色 [英] Changing QPushButton Color when Pressed

查看:3014
本文介绍了按下时改变QPushButton颜色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有办法改变一个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的 pressed()信号,并使用所需的颜色更新调色板,然后连接到 released()信号,并将颜色切换回旧颜色。

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.

您可能想要运行 update()设置颜色后或在极端情况下重绘( )

You may want to run update() after settings the colors or in extreme cases repaint().

这篇关于按下时改变QPushButton颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆