Signals and slots in two different classes
I'm learning about signals and slots and I'm confused about this situation.
The context: I have a method in ClassA who needs to emit a signal (from the worker thread) to ClassB (in the main thread). The worker thread does some heavy duty processing, while the main thread updates a progress bar in the UI. I need the worker thread to pass signals to the main thread to update the UI.
I'm not sure how to have a signal be passed between two different classes. Right now I'm having the first class instantiate a signal object (Communicate), connect it to a method in the second class, and then emit when ready. This is causing a seg fault, so clearly I'm doing something wrong.
I have the following signal class:
class Communicate(QtCore.QObject): update_progress = QtCore.Signal()
Here is the method in ClassA, the worker thread:
def methodInClassA(self, filename): c = Communicate() c.update_progress.connect(ClassB.set_progress) print "emitting" c.update_progress.emit() ...
Here is the method in ClassB, the one updating the progress bar in the UI:
class ClassB(QtGui.QDialog, classB.Ui_Dialog): def __init__(self, parent=None): super(ClassB, self).__init__(parent) self.setupUi(self) def set_progress(self): print "RECEIVED" sender = self.sender() self.progressBar.setValue(100)
You are connecting c.update_progress to ClassB.set_progress, an unbound method. That doesn't work in PySide and I'm pretty sure it won't work in PyQt either. You need to have an instance of ClassB and connect c.update_progress to its set progress method, something like c.update_progress.connect(i_am_b.set_progress)
i_am_b must have been instantiated elsewhere. You need to be careful to set the thread affinities for i_am_b and the instance of A before you call emit; see the docs for QObject.moveToThread.