import qt
import time

def trig():
    working.start()
    broken.start()

class Working(qt.QThread):
    def run(self):
        print "Working"
        qEvent = qt.QCustomEvent(1234)
        qt.qApp.postEvent(qt.qApp, qEvent)
        # wait to make sure main thread has handled the event and this function is
        # the only place having a reference to it
        time.sleep(0.5)
        print "deleting event"
        del qEvent
        print "deleted event\n"

class Broken(qt.QThread):
    def run(self):
        # let working finish
        time.sleep(1)
        print "Broken"
        qEvent = qt.QCustomEvent(1234)
        print "deleting event"
        del qEvent
        print "deleted event"

qApp = qt.QApplication([])

working = Working()
broken = Broken()

# make sure code isn't run until event thread has started
qt.QTimer.singleShot(0, trig)
# exit application when done
qt.QTimer.singleShot(2500, qt.qApp.exit)

qApp.exec_loop()

print "No problems if you've come this far"
