2

我想阻止 QPushButton 在单击时缩进,而只是更改它的背景颜色。

提前喝彩。

4

4 回答 4

2

一个易于实施(但可能无法完全提供您想要的)的解决方案是使用Qt Style Sheet

使用样式表,您可以为按钮的按下状态指定背景颜色。这样做会抑制缩进。例如 :

pMyPushButton->setStyleSheet("QPushButton:pressed { background-color : red; }");
于 2012-04-26T05:38:24.490 回答
1

至于停止缩进,我认为您可以按照此处记录的方式执行 setFlat:http: //doc.qt.io/archives/qt-4.7/qpushbutton.html#flat-prop

于 2012-04-26T03:19:38.283 回答
0

一种选择是继承 QPushButton 并使用您自己的实现覆盖其 paintEvent(QPaintEvent *) 方法,使其具有您想要的外观。

或者,您可以创建 QCommonStyle(或 QWindowsStyle 等)的子类并将其传递给 qApp->setStyle(),并覆盖 drawControl() 方法,以便当使用元素 CE_PushButtonBevel 调用 drawControl 时,它会使用调用超类一个单独的 QStyleOptionButton 参数,不包括 State_Sunken 或 State_On 位集。(可能需要对超类的 drawControl() 方法进行一些检查才能准确确定获得所需效果所需的条件)。

于 2012-04-26T03:42:21.090 回答
0

它帮助我padding:在样式表中设置属性。

QPushButton *cmd = new QPushButton();
cmd->setStyleSheet("padding: 0px;");

该值在某种程度上不相关。0px具有相同的效果4px。我不知道为什么。

像这样设置按下的属性:

cmd->setStyleSheet("QPushButton {background: blue; padding: 0px;}"
                   "QPushButton:pressed {background: orange;}");

另外我设置border: 1px solid black; outline: none;- 以防万一这相关。

于 2018-07-19T08:14:37.473 回答