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

Jeff Donner jeffrey.donner at gmail.com
Mon Sep 3 05:25:27 BST 2007


Hi,

 I just wanted to save someone else some time - in Qt's C++ gradient
demo, they set a QPainter directly with one of their gradients (as the
docs show as permitted), but that doesn't seem to work in PyQt:

           g = QtGui.QRadialGradient(midpoint.x, midpoint.y, self.blob_radius)

           opaque      = QtGui.QColor(value, 0.0, 0.0, 1.0)
           transparent = QtGui.QColor(value, 0.0, 0.0, 0.0)

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

           g.setSpread(QtGui.QGradient.PadSpread)

#            b = QtGui.QBrush(g)

           painter.setBrush(g)


Produces the error:

   painter.setBrush(g)
TypeError: argument 1 of QPainter.setBrush() has an invalid type


instead, do an extra step of making a brush with the gradient

 b = QtGui.QBrush(g)

and then set the brush.

 painter.setBrush(b)

I hope this saves someone time.


More information about the PyQt mailing list