[PyQt] how to set a widget background?

Marco marco at waven.com
Mon Aug 6 03:13:09 BST 2007


Hi,
  I write a widget from QWidget, and I want to change its backgound, I
change it via rewrite paintEvent, but it got my computer slowly. Is
there better method to change a widget backgourd?
 Thank you!

 here is my paintEvent code:

    def paintEvent(self, event):

        rectPath = QtGui.QPainterPath()
        rectPath.moveTo(0, 0)
        rectPath.lineTo(0, MAX_Y)
        rectPath.lineTo(MAX_X, MAX_Y)
        rectPath.lineTo(MAX_X, 0)
        rectPath.closeSubpath()

        painter = QtGui.QPainter()
        painter.begin(self)
        painter.setRenderHint(QtGui.QPainter.Antialiasing)
        painter.scale(self.width() / 100.0, self.height() / 100.0)
        painter.translate(50.0, 50.0)
        painter.rotate(0)
        painter.translate(-50.0, -50.0)

        painter.setPen(QtGui.QPen(QtGui.QColor(0, 0, 0), 0,
                                  QtCore.Qt.SolidLine, QtCore.Qt.RoundCap,
                                  QtCore.Qt.RoundJoin))
        gradient = QtGui.QLinearGradient(0, 0, 0, 100)
        gradient.setColorAt(0.0, QtGui.QColor(27, 152, 195))
        gradient.setColorAt(1.0, QtGui.QColor(22, 120, 173))
        painter.setBrush(QtGui.QBrush(gradient))
        painter.drawPath(rectPath)
        painter.end()


-- 
LinuX Power


More information about the PyQt mailing list