如何设置QPushButton的选中状态以去除灰点?

K--*_*K-- 7 css windows qt stylesheet qpushbutton

我使用的是Qt 5.3.0.当我在QPushButton的选中状态上应用一些背景颜色时,按钮将在选中时用灰色点(我想要的背景颜色)填充.

这是一个很小的测试程序(使用qtcreator,但它也可以通过编码完成):1,创建一个qt应用程序2,拖入QPushButton,将其设置为flat并检查3,在w.show()之前添加这些行

w.setStyleSheet("\
                QPushButton {   \
                    color:white;    \
                }   \
                QPushButton:checked{\
                    background-color: rgb(80, 80, 80);\
                }\
                QPushButton:hover{  \
                    background-color: grey; \
                    border-style: outset;  \
                }  \
                ");
Run Code Online (Sandbox Code Playgroud)

4,运行应用程序并检查按钮

你会看到按钮变成了点缀但是我需要选中的按钮是纯色的rgb(80,80,80).我错过了什么?

Ant*_*ias 16

我可以通过简单地设置样式表border: none;QPushButton:checked属性来删除点.

在你的例子中,它应该是这样的:

w.setStyleSheet("\
                QPushButton {   \
                    color:white;    \
                }   \
                QPushButton:checked{\
                    background-color: rgb(80, 80, 80);\
                    border: none; \
                }\
                QPushButton:hover{  \
                    background-color: grey; \
                    border-style: outset;  \
                }  \
                ");
Run Code Online (Sandbox Code Playgroud)

在这里,您可以在选中按钮时看到结果:

在此输入图像描述

  • 在该页面上:http://qt-project.org/doc/qt-4.8/stylesheet-reference.html有一个警告:"如果您只在QPushButton上设置背景颜色,除非您设置背景,否则背景可能不会出现这是因为,默认情况下,QPushButton会绘制一个与背景颜色完全重叠的原生边框." 我记得在某些情况下删除边框解决了我的问题.我无法确切地告诉你原因,但它确实有效. (6认同)