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

Eric Frederich eric.frederich at gmail.com
Tue Jun 28 20:42:51 BST 2011

Here is a reworked example.
Do you see any potential problems with this one?
Is the call to setText okay?
Is it okay to access the data member the way I'm doing it in print_data?


from PyQt4.QtCore import *
from PyQt4.QtGui  import *

class Blah(QThread):
    def __init__(self, parent=None):
        super(Blah, self).__init__(parent)
        self.data = []

    def run(self):
        import time
        for i in xrange(10):
            txt = '%02d' % i
            self.emit(SIGNAL('SLEPT'), txt)

from functools import partial

class MyDialog(QDialog):

    def __init__(self, *args, **kwargs):
        super(MyDialog, self).__init__(*args, **kwargs)
        self.setWindowTitle("Hey, hows it going?")

        layout = QVBoxLayout()
        for i in xrange(15):
            button = QPushButton("Push Me", self)
            self.connect(button, SIGNAL("pressed()"), self.do_something)


    def do_something(self):
        b = Blah(self)
        self.connect(b, SIGNAL('ALLDONE'),
partial(self.sender().setText, "Push Me"))
        self.connect(b, SIGNAL('ALLDONE'), partial(self.print_data, b))
        self.connect(b, SIGNAL('ALLDONE'),
partial(self.sender().setEnabled, True))
        self.connect(b, SIGNAL('SLEPT'), self.sender().setText)

    def print_data(self, blah):
        print blah.data

if __name__ == '__main__':
    import sys
    app = QApplication(sys.argv[1:])
    md = MyDialog()

On Tue, Jun 28, 2011 at 3:05 PM, Hans-Peter Jansen <hpj at urpla.net> wrote:
> 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.
> Pete
> _______________________________________________
> PyQt mailing list    PyQt at riverbankcomputing.com
> http://www.riverbankcomputing.com/mailman/listinfo/pyqt

More information about the PyQt mailing list