QtDesigner 5.15.8中按钮背景色显示似透明的原因咨询
问题原因
QPushButton在Windows等平台默认启用系统原生按钮样式,这套样式的渲染优先级高于自定义的background-color属性,导致直接设置背景色后,按钮看起来像是半透明或未完全生效。而QFrame作为基础容器控件,没有这类原生样式的覆盖逻辑,所以能正常显示设置的背景色。
解决方法
方法1:重置按钮边框,覆盖原生渲染
在样式表中为QPushButton添加border: none;,彻底禁用原生边框及关联的背景渲染逻辑,让background-color完全生效:
QPushButton{ background-color: red; border: none; } QFrame{ background-color: red; }
如果需要保留边框,可以自定义边框样式,比如:
QPushButton{ background-color: red; border: 1px solid #000; border-radius: 3px; }
方法2:使用background简写属性替代background-color
直接使用background属性,有时候能强制覆盖原生样式的渲染规则:
QPushButton{ background: red; } QFrame{ background-color: red; }
方法3:设置按钮为Flat样式
在QtDesigner的属性编辑器中,将QPushButton的flat属性设为true,按钮会去掉原生立体样式,此时背景色即可正常显示。若需要边框,可再通过样式表手动添加。
内容的提问来源于stack exchange,提问作者cagri cihanoglu
相关产品推荐
相关产品推荐

