I have a fairly complicated piece of code using PyQt4 and sometimes my signal callbacks simply never run.
It seems to be related to where/when I register the callbacks, ie. self.someobj.somesig.connect(self.callback).
I can see in the debugger that the callback is connected and that the signal es emitted.
The code is too large to post here, but I will try to summarize:
class RC(QtCore.QObject):
render_ready = QtCore.pyqtSignal(object)
def __init__(self, pc, ...):
super(RC, self).__init__()
self.pc = pc
self.pc.rr.new_render.connect(self.insert)
def check(self):
...
if a: self.pc.replot_request.emit()
else: self.pc.render_request.emit()
def insert(self, r):
...
if b: self.render_ready.emit(r)
self.check()
class RR(QtCore.QObject):
new_render = QtCore.pyqtSignal(object)
def __init__(self, pc, app,...):
super(RR, self).__init__()
self.app = app
self.pc = pc
def run(self):
self.pc.replot_request.connect(self.on_replot_request)
self.pc.render_request.connect(self.render)
...
def render(self, scale):
...
r = R(i)
r.moveToThread(QtGui.QApplication.instance().thread())
r.new_render.emit(r)
class R(QtCore.QObject):
def __init__(self, i):
super(R, self).__init__()
...
...
class PC(QtCore.QObject):
render_request = QtCore.pyqtSignal(int)
replot_request = QtCore.pyqtSignal(object)
def __init__(self, container, app):
super(PC, self).__init__()
...
self.rr = RR(self.app)
self.rr_thread = QtCore.QThread()
self.rr.moveToThread(self.rr_thread)
self.connect(self.rr_thread, QtCore.SIGNAL("started()"), self.rr.run)
self.c = RC(self)
...
self.c.render_ready.connect(self.on_nr)
def start(self):
self.rr_thread.start()
def on_nr(self, r):
print "on_nr(...)"
class App(QtCore.QObject):
...
def __init__(self):
..
self.pc = PC(container, app)
...
self.pc.start(self) # Last thing in constructor
X = QtGui.QApplication(sys.argv)
a = App()
sys.exit(X.exec_())
I see render_ready being connected to on_nr and the render_ready being emitted. But on_nr never runs. The signal sequence is complicated, it goes across threads, but the signal and callback in question are in the same thread. Also, the event loop is clearly running. How could I start digging deeper? Or am I making some evident mistake? Thanks.
RCyou referenceself.pc.rbut I can't see that defined anywhere. InRR.renderyou create a new instance ofRand emit a signal, but you don't have the definition forRanywhere) - three_pineapplesRis just a simple subclass ofQtCore.QObject. I'm updating the example now. - jpcgtRR.render. You create an instance ofR, move it to a different thread (why?) and emit a signal that doesn't appear to be connected to anything. Meanwhile,RC.render_readyisn't emitted untilRC.insertis called, which happens whenPC.r.new_renderis emitted, but that object (PC.r) doesn't seem to exist in your code. So I'm not sure if you have simplified your code too much, or if this is the root cause of your problem. I know making a minimal reproducible example will mean you are likely to solve your problem, but quite frankly, that is the point. - three_pineapplesPC.rchanged toPC.rr. I was able to solve the problem and unfortunately it had nothing to do with PyQt and the use of signals. I tried an MVCE, but as I suspected would happen, the problem did not show up, because I did not include the cause... I had not pinpointed it yet. See my answer below. Thanks anyway. - jpcgt