[PyQt] Signals/Slots old/new
Vayu
vayu at sklinks.com
Sun Jun 24 19:16:03 BST 2012
In trying to send values back and forth from a main window to a docked widget I have questions about what ended up working, especially regarding examples from web searches showing old vs new way.
Did I do it the best way when using the new way? And why do I want to use the new way?
I had to create a separate class subclassed from QObject in order to create a signal and specify that it had arguments like this:
class Messenger(QtCore.QObject):
fromWidgetSignal = QtCore.pyqtSignal(['QString'])
fromMainWindowSignal = QtCore.pyqtSignal(['QString'])
Connect and emit like this in the main window:
self.messageSender = Messenger()
self.messageSender.fromWidgetSignal['QString'].connect(self.receiveValue)
self.messageSender.fromMainWindowSignal['QString'].connect(self.theWidget.receiveValue)
self.messageSender.fromMainWindowSignal.emit("Main Window sends a value.")
and the widget needed to receive the new message class as a parameter to send:
def __init__(self, messageSender):
self.messageSender = messageSender
self.messageSender.fromWidgetSignal.emit("Widget sends a value.")
The old way seemed simpler to me:
connect and emit in the main window like this:
self.fromWidgetSignal['QString'].connect(self.receiveValue)
self.fromMainWindowSignal['QString'].connect(self.theWidget.receiveValue)
self.emit(QtCore.SIGNAL("fromMainWindowSignal"), "Main Window sends a value.")
and in the widget like this:
self.emit(QtCore.SIGNAL("fromWidgetSignal"), "Widget sends a value.")
No extra class and no sending parameters. Did I do the new way right and if so, why do I want to?
More information about the PyQt
mailing list