【问题标题】:How to style QPushButton's checked state to remove grey dots?如何设置按钮选中状态的样式以删除灰点?
【发布时间】:2014-07-13 01:13:39
【问题描述】:

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

这是一个小型测试程序(使用 qtcreator,但也可以通过编码完成): 1、创建一个qt应用 2、拖入一个QPushButton,设置为flat and checkable 3、在w.show()之前添加这几行

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

4、运行应用并检查按钮

您会看到按钮变为点状,但我需要选中的按钮为纯色 rgb(80, 80, 80)。 我错过了什么吗?

【问题讨论】:

    标签: css windows qt stylesheet qpushbutton


    【解决方案1】:

    我可以通过在样式表的QPushButton:checked 属性上简单地设置border: none; 来删除这些点。

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

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

    在这里你可以看到按钮被选中时的结果:

    【讨论】:

    • 在该页面上:qt-project.org/doc/qt-4.8/stylesheet-reference.html 有一个警告:“如果您只在 QPushButton 上设置背景颜色,则背景可能不会出现,除非您将边框属性设置为某个值。这是因为,默认情况下,QPushButton 绘制一个与背景颜色完全重叠的原生边框。”我记得在某些情况下删除边框可以解决我的问题。我不能告诉你确切的原因,但它确实有效。
    • 感谢您的回答和参考链接。今天真的救了我!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-07-15
    • 1970-01-01
    • 2021-05-03
    • 1970-01-01
    • 2012-07-29
    • 1970-01-01
    相关资源
    最近更新 更多