import sys
from PyQt4 import QtGui, QtCore

class Report(QtCore.QObject):
    def report(self):
        print "In report() in thread", QtCore.QThread.currentThread()

class WorkerThread(QtCore.QThread):
    def __init__(self):
        QtCore.QThread.__init__(self)
    def slotDo(self):
        print 'slotDo called in thread:', self.currentThread()
        self.emit(QtCore.SIGNAL('done'))
    def run(self):
        self.report = Report()
        self.connect(self, QtCore.SIGNAL('done'), self.report.report)

        print 'Worker thread:', self.currentThread()
        self.exec_()

class MainWindow(QtGui.QPushButton):
    def __init__(self):
        self.i = 1
        QtGui.QPushButton.__init__(self, 'Do %i' % self.i)
        self.connect(self, QtCore.SIGNAL('clicked()'), self.slotClicked)
    def slotClicked(self):
        self.emit(QtCore.SIGNAL('do'))
    def slotDone(self):
        self.i += 1
        self.setText('Do %i' % self.i)

app = QtGui.QApplication(sys.argv)
thr = app.thread()
win = MainWindow()
win.show()
worker = WorkerThread()
worker.start()
app.connect(win, QtCore.SIGNAL('do'), worker.slotDo)
app.connect(worker, QtCore.SIGNAL('done'), win.slotDone)
print 'Main thread:', thr
app.exec_()
