Retaining drawing across paintevents

Chris Angelico rosuav at gmail.com
Wed Jan 1 10:05:56 EST 2014


On Thu, Jan 2, 2014 at 1:53 AM,  <angedward3 at gmail.com> wrote:
> I find that I have to re-paint the whole widget every time I call an update(), as I have create a new QPainter() instance. Is there a way to update only a small part of the widget, while retaining the rest of the widget?

In general, it would help to say which windowing toolkit you're using
:) Fortunately you mention something that implies you're using QT, so
I'm going to forge ahead with that.

When you call update(), you're effectively saying "the whole object
needs to be repainted". If you can restrict that to just a particular
rectangle, simply pass those coordinates to the update call:

http://pyqt.sourceforge.net/Docs/PyQt4/qwidget.html#update-4

I'm not sure if you're using PyQt4 or not, but if you're using
something else, look through its docs for a similar function. Same
goes for pretty much any windowing toolkit; it's always possible to
report that a small area needs updating.

Once you then get the update event, you should be given a rectangle
that tells you which part needs to be repainted. But painting outside
that area will be reasonably fast, so don't stress too much about that
part if it's a lot of trouble. For instance, I have a MUD client that
will always attempt to draw complete lines, even if only part of a
line needs to be redrawn - but it'll repaint only those lines which
need repainting (so it looks at the Y coordinates of the "please
repaint me" rectangle, but not the X coords).

ChrisA



More information about the Python-list mailing list