QWidget不绘制背景颜色

9 投票
1 回答
6765 浏览
提问于 2025-04-18 02:56

我正在使用 PySide 1.2.1 和 Python 2.7,需要一个可以绘制彩色背景的组件。在 Qt Designer 中,我创建了一个简单的窗口,里面有一个标签,一个包含三个其他项目的组件,还有另一个标签。对于包含按钮、单选按钮和复选框的组件,我把 styleSheet 属性设置为 background-color: #FFFFFF,也就是白色背景。

在 Qt Designer 中,一切看起来都很正常:

Qt Designer中的窗口

但是在 PySide 中,这个组件并没有绘制背景颜色,不过上面的项目却正确地继承了颜色:

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>

这是我的 Python 代码,没做什么特别的事情:

import sys

from PySide import QtCore, QtGui

from generated.test import Ui_MainWindow

class MainWindow(Ui_MainWindow,QtCore.QObject):

    def __init__(self, *args, **kwargs):
        Ui_MainWindow.__init__(self, *args, **kwargs)
        QtCore.QObject.__init__(self)

    def setupUi(self, MainWindow):
        Ui_MainWindow.setupUi(self, MainWindow)

def main(argv):
    app = QtGui.QApplication(argv)
    mainwindow = QtGui.QMainWindow()

    ui = MainWindow()
    ui.setupUi(mainwindow)

    mainwindow.show()
    sys.exit(app.exec_())

if __name__ == "__main__":
    main(sys.argv)

我已经尝试过 self.widget.setAutoFillBackground(True),但根据文档,只要背景有有效的 styleSheet 值,这个属性就会被禁用。

这个方法也不行:

p = self.widget.palette()
p.setColor(self.widget.backgroundRole(), QtCore.Qt.white)
self.widget.setPalette(p)

(这些提示来自如何设置 QWidget 背景颜色?

我该如何让这个组件绘制白色背景呢?

1 个回答

16

在容器小部件上设置 WA_StyledBackground 属性:

ui = MainWindow()
ui.setupUi(mainwindow)
ui.widget.setAttribute(QtCore.Qt.WA_StyledBackground, True)

(顺便说一下,出于性能考虑,并不是所有的小部件类默认都会设置这个属性——不过文档里似乎没有说明具体是哪些类)。

撰写回答