[PyQt] Signals/Slots old/new

Phil Thompson phil at riverbankcomputing.com
Sun Jun 24 22:27:12 BST 2012


On Sun, 24 Jun 2012 11:16:03 -0700, Vayu <vayu at sklinks.com> wrote:
> 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'])

      fromWidgetSignal = QtCore.pyqtSignal('QString')

>     fromMainWindowSignal = 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.fromWidgetSignal.connect(self.receiveValue)

>    
self.messageSender.fromMainWindowSignal['QString'].connect(self.theWidget.receiveValue)

     
self.messageSender.fromMainWindowSignal.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? 

The new way will give you diagnostics if you make a mistake. With the old
way if, for example, you misspelt the signal name then it would not work
but it won't tell you.

In the new way the signals are proper Qt signals - visible to C++ code
that knows nothing about Python and Qt Designer.

Phil


More information about the PyQt mailing list