[PyQt] QPainter::end: Painter ended with 2 saved states

Hans-Peter Jansen hpj at urpla.net
Tue Jun 28 20:05:20 BST 2011

On Tuesday 28 June 2011, 20:52:12 Eric Frederich wrote:
> I was trying to get an example working with a QThread.
> I wound up creating one but after running it for a while and pressing
> buttons I got the error...
> QPainter::end: Painter ended with 2 saved states
> Am I doing something wrong?
> I wanted an example where a worker thread would query a database or
> some other long running operation and leave the GUI responsive
> (although disabling certain elements like the button it was launched
> from).
> This is the code I was running....
> from PyQt4.QtCore import *
> from PyQt4.QtGui  import *
> class Blah(QThread):
>     def __init__(self, parent=None):
>         super(Blah, self).__init__(parent)
>         print 'new thread created'
>     def run(self):
>         print 'running'
>         self.parent().setEnabled(False)
>         import time
>         for i in xrange(10):
>             self.parent().setText("%02d" % i)
>             time.sleep(.1)
>         self.parent().setText("Push Me")
>         self.parent().setEnabled(True)

You're doing "bad" things here, that you shouldn't do from a thread, 
e.g. using methods, that trigger redraws. You should create signals in 
your thread, that signals operations to the main thread, where you 
would actually process these operations without fear.


More information about the PyQt mailing list