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

Eric Frederich eric.frederich at gmail.com
Tue Jun 28 19:52:12 BST 2011


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)

def say_hi():
    print 'HELLO'

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

        self.buttons = []
        for i in xrange(5):
            button = QPushButton("Push Me")
            self.buttons.append(button)
            blah = Blah(button)
            self.connect(button, SIGNAL("pressed()"), blah.start)
            layout.addWidget(button)

        self.setLayout(layout)

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


More information about the PyQt mailing list