python - QWidget不绘制背景色

标签 python pyside background-color qwidget qtstylesheets

我正在使用 PySide 1.2.1 和 Python 2.7,我需要一个小部件来绘制彩色背景。 在 Qt Designer 中,我创建了一个简单的窗口,其中包含一个标签、一个包含三个其他项目的小部件和另一个标签。对于包含按钮、单选按钮和复选框的小部件,我将 styleSheet 属性设置为 background-color: #FFFFFF。 在 Qt Designer 中,一切都按需要呈现:

Window in Qt Designer

但在 Pyside 中,小部件不会绘制背景颜色 - 但其上的项目会正确继承颜色:

Window in 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),但根据 the documentation只要有一个有效的背景样式表值,这个属性就会被禁用。

这也行不通:

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

(从 How to set QWidget background color? 得到这些提示)

如何让小部件绘制白色背景颜色?

最佳答案

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

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

(PS:出于性能原因,默认情况下并未为某些小部件类设置此属性 - 但文档似乎并未指定哪些类)。

关于python - QWidget不绘制背景色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23104051/

相关文章:

python - 如何通过fiddler2捕获python SSL(HTTPS)连接

python - 如何在我的 GUI 上绘图

Python 'called with the wrong argument type' 错误

javascript - 使用 JavaScript 将多行内联元素分解为不同的元素

c# - Infragistics - 设置 Excel 单元格背景色

Javascript 可以将背景颜色更改为某些 <div>

python - 从实时 Google App Engine 应用程序导出数据的最简单方法是什么?

python - 排列嵌套的 dict 项 Python

结合范围和数字列表的python

python - PyQt 获取ListWidget中的特定值