First set the Autofillbackground property to True
Then define a Qpalette object
Set the Background property (color or picture) of a Qpalette object
Finally set the palette of the Qwidget object
Instance:
1 #-*-coding:utf-8-*-2 ImportSYS3 fromPyQt4ImportQtgui4 fromPyqt4.qtguiImport*5 fromPyqt4.qtcoreImport*6 7 8 classIcon (qtgui.qwidget):9 def __init__(Self, parent=None):TenQtgui.qwidget.__init__(self, parent) OnePalette1 =Qtgui.qpalette (self) APalette1.setcolor (Self.backgroundrole (), Qcolor (192,253,123))#Set Background color - #Palette1.setbrush (Self.backgroundrole (), Qtgui.qbrush (Qtgui.qpixmap (' ... /.. /.. /document/images/17_big.jpg ')) # Set the background picture - Self.setpalette (palette1) the Self.setautofillbackground (True) # does not set or can -Self.setgeometry (300, 300, 250, 150) -Self.setwindowtitle ('Icon') -Self.setwindowicon (Qtgui.qicon ('.. /.. /.. /document/images/firefox.png')) +Mylayout =qvboxlayout () - self.setlayout (mylayout) + A at - -App =qtgui.qapplication (SYS.ARGV) -icon =Icon () - icon.show () -Sys.exit (App.exec_ ())
Effect:
PYQT setting background color and background image