Issue
I thought I've read it all about threading in Qt, but apparently I'm getting it wrong. I'm stuck with this stuff for some time now, so I would really appreciate your help a lot.
So, I created a class that starts a thread in its __init__ method:
class MyClass(object):
def __init__(self):
(...)
self.thread = QtCore.QThread(parent)
worker = Worker()
QtCore.QObject.connect(self.thread, QtCore.SIGNAL('started()'), worker,
QtCore.SLOT('doWork()'))
QtCore.QObject.connect(worker, QtCore.SIGNAL('finished()'), self.thread,
QtCore.SLOT('quit()'))
QtCore.QObject.connect(worker, QtCore.SIGNAL('finished()'), worker,
QtCore.SLOT('deleteLater()'))
QtCore.QObject.connect(self.thread, QtCore.SIGNAL('finished()'),
self.thread, QtCore.SLOT('deleteLater()'))
worker.moveToThread(self.thread)
self.thread.start()
Worker class looks like that:
class Worker(QtCore.QObject):
(some signals)
def doWork(self):
print "doing my work"
The problem is that my doWork slot is never executed, although the thread is running.
After creating instance of MyClass, let's say: obj = MyClass()
I can call: obj.thread.isRunning()
which returns True.
I assume that signal 'started' is not emitted, because of instantly exiting the method where thread was created (when I add sleep after starting thread, it enters doWork slot). I wonder how such situation should be handled properly.
Please let me know if I did not explain my problem clearly. Thank you all for help in advance.
Solution
Your worker is being garbage collected after the MyClass
constructor returns. Make worker
a member of MyClass
so that it persists beyond the constructor.
self.worker = Worker()
Answered By - user3419537
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.