<div dir="ltr"><div class="markdown-here-wrapper" style><p style="margin:1.2em 0px!important">All,</p>
<p style="margin:1.2em 0px!important">I’m confused when it comes to the proper way to mesh Qt’s model of creating a tree of parented objects which destroy themselves when no longer used with Python’s “delete it when I feel like it” garbage collector. In particular, I’d like to shut down threads when the underlying Qt objects are destroyed, but I can’t figure out how to do that. Any advice and wisdom would be appreciated. I've attached same code with my thoughts.</p>
<p style="margin:1.2em 0px!important">Bryan</p>
<pre style="font-size:0.85em;font-family:Consolas,Inconsolata,Courier,monospace;font-size:1em;line-height:1.2em;margin:1.2em 0px"><code style="font-size:0.85em;font-family:Consolas,Inconsolata,Courier,monospace;margin:0px 0.15em;padding:0px 0.3em;white-space:pre-wrap;border:1px solid rgb(234,234,234);border-top-left-radius:3px;border-top-right-radius:3px;border-bottom-right-radius:3px;border-bottom-left-radius:3px;display:inline;background-color:rgb(248,248,248);white-space:pre;overflow:auto;border-top-left-radius:3px;border-top-right-radius:3px;border-bottom-right-radius:3px;border-bottom-left-radius:3px;border:1px solid rgb(204,204,204);padding:0.5em 0.7em;display:block!important"># Test program to ask about destructors and PyQt4.
import sys, time
from PyQt4.QtGui import QApplication
from PyQt4.QtCore import QThread, QTimer, QObject
class Worker(QObject):
def run(self):
time.sleep(1)
class ThreadController(QObject):
def __init__(self, parent):
QObject.__init__(self, parent)
# No parent object provided; if so, we get the error
# "QObject::moveToThread: Cannot move objects with a parent".
# Who will deallocate this object, since it has no parent? I
# assume the answer is "Python, maybe". Is that good enough?
self.worker = Worker()
self.workerThread = QThread(self)
self.worker.moveToThread(self.workerThread)
# Use method #2 below. Commnet out for a crash.
parent.aboutToQuit.connect(self.del_)
self.workerThread.start()
# I'd like to run when when ~ThreadController is invoked. That's quite
# differentfrom when __del__ is invoked. How?
#
# 1. Give up. Just invoke it manually. However, this is error-prone (i.e.
# I'll forget to do it at some point).
# 2. Connect app.aboutToQuit to this. This seems fairly reasonable, but
# it would feel cleaner to invoke when the destructor is invoked,
# rather than earlier. Also, this means that the QApplication would
# need to be passed to every thread, which might involve awkwardness
# in passing it through several intervening subclasses.
# 3. Connect destroyed(), which doesn't work -- this isn't emitted until
# the object is mostly dead, far after ~ThreadController was invoked,
# and won't be executed.
# 4. Have __del__ invoke this, which doesn't work -- it only happens after
# ~ThreadController has finihed.
def del_(self):
print('del_')
self.workerThread.quit()
self.workerThread.wait()
def __del__(self):
print('__del__')
# Crash when uncommented.
#self.del_()
if __name__ == '__main__':
app = QApplication(sys.argv)
# Exit the program shortly after the event loop starts up, but before the
# thread finishes.
QTimer.singleShot(200, app.exit)
# Start a thread
tm = ThreadController(app)
# Run the main event loop.
ret = app.exec_()
print('done')
sys.exit(ret)
</code></pre><div title="MDH:QWxsLDxkaXY+PGJyPjwvZGl2PjxkaXY+SSdtIGNvbmZ1c2VkIHdoZW4gaXQgY29tZXMgdG8gdGhl
IHByb3BlciB3YXkgdG8gbWVzaCBRdCdzIG1vZGVsIG9mIGNyZWF0aW5nIGEgdHJlZSBvZiBwYXJl
bnRlZCBvYmplY3RzIHdoaWNoIGRlc3Ryb3kgdGhlbXNlbHZlcyB3aGVuIG5vIGxvbmdlciB1c2Vk
IHdpdGggUHl0aG9uJ3MgImRlbGV0ZSBpdCB3aGVuIEkgZmVlbCBsaWtlIGl0IiBnYXJiYWdlIGNv
bGxlY3Rvci4gSW4gcGFydGljdWxhciwgSSdkIGxpa2UgdG8gc2h1dCBkb3duIHRocmVhZHMgd2hl
biB0aGUgdW5kZXJseWluZyBRdCBvYmplY3RzIGFyZSBkZXN0cm95ZWQsIGJ1dCBJIGNhbid0IGZp
Z3VyZSBvdXQgaG93IHRvIGRvIHRoYXQuIEFueSBhZHZpY2UgYW5kIHdpc2RvbSB3b3VsZCBiZSBh
cHByZWNpYXRlZC48L2Rpdj48ZGl2Pjxicj48L2Rpdj48ZGl2PkJyeWFuPC9kaXY+PGRpdj48YnI+
PC9kaXY+PGRpdj48ZGl2PiZuYnNwOyAmbmJzcDsgIyBUZXN0IHByb2dyYW0gd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7ICZuYnNwOyZuYnNwOzwvZGl2PjxkaXY+PGJyPjwv
ZGl2PjwvZGl2Pg==" style="height:0;font-size:0em;padding:0;margin:0"></div></div><div>-- <br>Bryan A. Jones, Ph.D.<br>Associate Professor<br>Department of Electrical and Computer Engineering<br>231 Simrall / PO Box 9571<br>Mississippi State University<br>Mississippi state, MS 39762<br><a href="http://www.ece.msstate.edu/~bjones" target="_blank">http://www.ece.msstate.edu/~bjones</a><br>bjones AT ece DOT msstate DOT edu<br>voice 662-325-3149<br>fax 662-325-2298<br><br>Our Master, Jesus Christ, is on his way. He'll show up right on<br>time, his arrival guaranteed by the Blessed and Undisputed Ruler,<br>High King, High God.<br>- 1 Tim. 6:14b-15 (The Message)<br>
</div></div>