[PyQt] can't create a QPainter in QTreeWidget's paintEvent

David Boddie david at boddie.org.uk
Thu Aug 23 16:29:05 BST 2007


On Thu Aug 23 16:04:19 BST 2007, Devon wrote:

> > If you pass a device (self in this case) when you create a QPainter, don't
> > call its begin() or end() methods. I think its the end() call that
> > actually triggers this message in this case.
> 
> I'm not explicitly calling begin() - I believe that's happening in 
> QPainter(self).  painter.end() is not the root cause.

OK. I just noticed the use of an explicit end() without an explicit begin()
and jumped to conclusions about the cause of the problem. Sorry about that.

Anyway, it looks like we're trying to paint onto the wrong device - the
QAbstractScrollArea documentation for its paintEvent() method says this:

  This event handler can be reimplemented in a subclass to receive paint
  events (passed in event), for the viewport() widget.

  Note: If you open a painter, make sure to open it on the viewport().

So you write your paintEvent() to do this instead:

    def paintEvent(self, e):
        QTreeWidget.paintEvent(self, e)
        painter = QPainter(self.viewport())

David


More information about the PyQt mailing list