[PyQt] Re: Note about setting setting painters with gradients instead of brushes

Jeff Donner jeffrey.donner at gmail.com
Mon Sep 3 19:37:52 BST 2007


> This should work with PyQt v4.2 and later. If that's what you are using then
send me a simple script that demonstrates the problem.

Here - I believe I'm using the latest. I'm not using Ubuntu's old one
at least. I can't tell how, to see. Anyway, this shows it on my
system.

#!/usr/bin/env python

import sys

from PyQt4 import QtCore, QtGui

class MainWidget(QtGui.QWidget):
    def __init__(self, parent = None):
        QtGui.QWidget.__init__(self, parent)
        self.w = 10.0 ; self.h = 10.0

    def paintEvent(self, event):
        xpos = self.w / 2.0 ; ypos = self.h / 2.0
        radius = 3.0
        painter = QtGui.QPainter(self)

        # invert it (so it's right-side-up)
        factor = 1.5
        painter.setWindow(-((factor - 1.0) * self.w) / 2.0,
                           ((factor + 1.0) * self.h) / 2.0,
                           self.w * factor, -self.h * factor)

        painter.setPen(QtCore.Qt.black)
        # bottom, right, top, left
        painter.drawLine(0.0, 0.0, self.w, 0.0)
        painter.drawLine(self.w, 0.0, self.w, self.h)
        painter.drawLine(self.w, self.h, 0.0, self.h)
        painter.drawLine(0.0, self.h, 0.0, 0.0)


        g = QtGui.QRadialGradient(xpos, ypos, radius)

        opaque      = QtGui.QColor(255, 0, 0, 255)
        transparent = QtGui.QColor(255, 0, 0,   0)

        g.setColorAt(0.0, opaque)
        g.setColorAt(1.0, transparent)

        g.setSpread(QtGui.QGradient.PadSpread)

        workaround = False

        if workaround:
            print "workaround"
            b = QtGui.QBrush(g)
            painter.setBrush(b)
        else:
            print "no workaround"
            painter.setBrush(g)

        r = QtCore.QRectF(xpos - radius, ypos - radius, radius * 2, radius * 2)
        painter.setPen(QtCore.Qt.NoPen)
        painter.drawRect(r)



app = QtGui.QApplication(sys.argv)

widget = MainWidget()
widget.resize(800, 600)
widget.show()
sys.exit(app.exec_())


More information about the PyQt mailing list