1

I'm trying to create a QPushButton that's just got an icon and a constant background color. So that I can swap out the icon when the user clicks it, without any other apparent effects (this is for a roll-up/roll-down feature). I've added an entry like this to my stylesheet:

QPushButton.ToggleButton {
    background-color: #8af; 
}

and set the button's class to match, and this does indeed give me the look I want, except that when I click on it the background color changes to a lighter blue, which I don't want. What am I missing?

Edit: I guess I should mention I'm using Qt 4.5 and PyQt 4.6 to do this...

4

4 回答 4

1

回答

尝试使用 QObject::setObjectName 给按钮一个 ID,然后使用 #idSelector 应用样式?

在 Python 中,代码可能看起来像这样:

button = QPushButton(self)
button.setObjectName("ToggleButton")

和这样的样式表:

#ToggleButton:pressed {
  background-color: #8af;
}

进一步阅读

于 2009-11-16T19:49:45.657 回答
1

我知道人们喜欢使用样式表,但在这种情况下,我认为制作自定义按钮同样容易。定义一个继承自 的类QAbstractButton,并覆盖 paint() 方法。在paint方法中,用您想要的背景颜色填充矩形,然后在顶部绘制当前图标。如果您也想要按钮周围的边框,它可能会稍微复杂一些,但不是很多。

或者,您还可以查看 的角色QPalette,特别是QPalette::LightQPalette::Midlight,它们可用于在按下时调整按钮的颜色。

于 2009-11-11T14:52:19.013 回答
0

I'm guessing doing background-color: #8af !important; would be too obvious so I'm assuming that doesn't work. It's worth a try if you haven't done it yet.

Otherwise, as noted in this question, there are specific states you can style. Try setting the same background color for the pressed state:

QPushButton.ToggleButton:pressed { background-color: #8af; }

Sorry if I misunderstood. Hope that helps.

于 2009-11-11T00:28:47.260 回答
0

在 Qt 设计器中打开按钮的样式表并尝试以下操作:
QPushButton:pressed { image: url(/path/to/your/file/fileName.png); }

于 2009-11-13T07:03:42.900 回答