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

Phil Thompson phil at riverbankcomputing.co.uk
Mon Sep 3 09:09:06 BST 2007


On Monday 03 September 2007, Jeff Donner wrote:
> 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.

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.

Phil


More information about the PyQt mailing list