Python QWidget不绘制背景色
我将PySide1.2.1与Python2.7一起使用,我需要一个小部件来绘制彩色背景。 在Qt Designer中,我创建了一个简单的窗口,包括一个标签、一个包含三个其他项的小部件和另一个标签。对于包含按钮、单选按钮和复选框的小部件,我将Python QWidget不绘制背景色,python,pyside,background-color,qwidget,qtstylesheets,Python,Pyside,Background Color,Qwidget,Qtstylesheets,我将PySide1.2.1与Python2.7一起使用,我需要一个小部件来绘制彩色背景。 在Qt Designer中,我创建了一个简单的窗口,包括一个标签、一个包含三个其他项的小部件和另一个标签。对于包含按钮、单选按钮和复选框的小部件,我将样式表属性设置为背景色:#FFFFFF。 在Qt Designer中,所有内容都按需要渲染: 但在Pyside中,小部件不会绘制背景色-但其上的项目会正确继承颜色: 以下是ui XML: <?xml version="1.0" encoding="U
样式表
属性设置为背景色:#FFFFFF
。
在Qt Designer中,所有内容都按需要渲染:
但在Pyside中,小部件不会绘制背景色-但其上的项目会正确继承颜色:
以下是ui XML:
<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
<class>MainWindow</class>
<widget class="QMainWindow" name="MainWindow">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>276</width>
<height>133</height>
</rect>
</property>
<property name="windowTitle">
<string>MainWindow</string>
</property>
<widget class="QWidget" name="centralwidget">
<layout class="QVBoxLayout" name="verticalLayout" stretch="0,1,1">
<item>
<widget class="QLabel" name="label">
<property name="text">
<string>The following should have white background:</string>
</property>
</widget>
</item>
<item>
<widget class="QWidget" name="widget" native="true">
<property name="styleSheet">
<string notr="true">background-color: #FFFFFF</string>
</property>
<layout class="QHBoxLayout" name="horizontalLayout">
<item>
<widget class="QPushButton" name="pushButton">
<property name="text">
<string>PushButton</string>
</property>
</widget>
</item>
<item>
<widget class="QRadioButton" name="radioButton">
<property name="text">
<string>RadioButton</string>
</property>
</widget>
</item>
<item>
<widget class="QCheckBox" name="checkBox">
<property name="text">
<string>CheckBox</string>
</property>
</widget>
</item>
</layout>
</widget>
</item>
<item>
<widget class="QLabel" name="label_2">
<property name="text">
<string>But it hasn't :-(</string>
</property>
</widget>
</item>
</layout>
</widget>
<widget class="QMenuBar" name="menubar">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>276</width>
<height>18</height>
</rect>
</property>
</widget>
<widget class="QStatusBar" name="statusbar"/>
</widget>
<resources/>
<connections/>
</ui>
我已经尝试了self.widget.setAutoFillBackground(True)
,但是根据这个属性,只要背景有一个有效的样式表值,它就会被禁用
这也不起作用:
p = self.widget.palette()
p.setColor(self.widget.backgroundRole(), QtCore.Qt.white)
self.widget.setPalette(p)
(从中得到这些提示)
如何让小部件绘制白色背景色?在容器小部件上设置属性:
ui = MainWindow()
ui.setupUi(mainwindow)
ui.widget.setAttribute(QtCore.Qt.WA_StyledBackground, True)
(注:出于性能原因,某些小部件类默认情况下没有设置此属性,但文档似乎没有指定哪些类)。我不明白的是,两个
QWidget()都没有设置属性WA_StyledBackground
(方法testAttribute()
)
和QLabel
但是第一个需要为绘图样式背景设置,第二个不需要?(刚刚测试过)花了很长时间才找到这个。有趣的是,我从QWidget继承的一些自定义类的行为确实是这样的,而其他的则不是这样。
ui = MainWindow()
ui.setupUi(mainwindow)
ui.widget.setAttribute(QtCore.Qt.WA_StyledBackground, True)