设置 背景颜色和背景图片
首先设置autoFillBackground属性为真
然后定义一个QPalette对象
设置QPalette对象的背景属性(颜色或图片)
最后设置QWidget对象的Palette
实例:
1 #-*- coding: utf-8 -*-
2 importsys3 from PyQt4 importQtGui4 from PyQt4.QtGui import *
5 from PyQt4.QtCore import *
6
7
8 classIcon(QtGui.QWidget):9 def __init__(self, parent=None):10 QtGui.QWidget.__init__(self, parent)11 palette1 =QtGui.QPalette(self)12 palette1.setColor(self.backgroundRole(), QColor(192,253,123)) #设置背景颜色
13 #palette1.setBrush(self.backgroundRole(), QtGui.QBrush(QtGui.QPixmap('../../../Document/images/17_big.jpg'))) # 设置背景图片
14 self.setPalette(palette1)15 self.setAutoFillBackground(True) # 不设置也可以
16 self.setGeometry(300, 300, 250, 150)17 self.setWindowTitle('Icon')18 self.setWindowIcon(QtGui.QIcon('../../../Document/images/firefox.png'))19 mylayout =QVBoxLayout()20 self.setLayout(mylayout)21
22
23
24
25 app =QtGui.QApplication(sys.argv)26 icon =Icon()27 icon.show()28 sys.exit(app.exec_())
效果:
---------------------------------------------------------------------------------------
QPalette 调色板 与QPainter 画板区别
1 #-*- coding: