[PyQt] QPainter with "with" statement

Martin Teichmann lkb.teichmann at gmail.com
Tue Mar 11 17:27:49 GMT 2008


Hi list,

I was just wondering if it wasn't a good idea to add the with-statement
functionality to the QPainter class. If your program happens to 
raise an exception while QPainter is active, QPainter.end() might
never be called and that leads to a lot of annoying error messages.

I just propose to add to methods to QPainter, like that:

def __enter__(self):
    return self
def __exit__(self, info):
    self.end()
    return False

Then you can write code like this:

with QPainter(image) as p:
    p.drawWhatever()

and you never have the problem with a dangling QPainter.

I guess someone will start yelling "but that's Python 2.5". Indeed,
it is. But well, the changes I propose are perfectly backwards-compatible,
as it only means to add to methods to a class.

Any other thoughts / ideas?

Cheers,

Martin



More information about the PyQt mailing list