[PyQt] converting old signal/slot to new signal/slot

David Cortesi davecortesi at gmail.com
Wed May 27 20:30:19 BST 2015


I'll try to clarify the new slot/signal style a bit, hopefully somebody
else will fill in more.

To start, just forget the old style entirely. A signal is a property of an
object. It happens to have a method connect(). Thus

    input_line = QLineEdit()
    input_line.editingFinished.connect( self.accept_input_line )

The argument to connect is an executable, typically self.some_method but
often just a slot (i.e. method) on some other widget, as when you have an
Edit menu with an Undo action, and you do

    undo_action = my_edit_menu.addAction( QAction('Undo') )
    undo_action.triggered.connect( my_plain_text_editor.undo )

(The "slot' executable can even be a lambda! Which is sometimes quite
handy.)

You are trying to create a signal of your own. I don't think much of the
python central tutorial you linked, it seems oriented to PySide and doesn't
seem to match up with the PyQt5 doc, here:

    http://pyqt.sourceforge.net/Docs/PyQt5/signals_slots.html

Follow the syntax under "Defining new signals with pyQtSignal". You are
defining a class property, i.e. declared at the top level of a class
definition, and what is not made explicit in that writeup, is that the
class must derive from QObject (not python's object). Once you have defined
the signal, e.g.

    class myWidget( QWidget ):
        databaseChanged = pyQtSignal()

it is a property of the class ergo of every object of the class. In the
__init__ probably you would do something like,

    self.databaseChanged.connect( self.react_to_db )

Or possibly in some OTHER object's __init__ it creates a myWidget and
connects it:

    new_widgy = myWidget(blah blah)
    new_widgy.databaseChanged.connect( self.catch_new_widgy )

When it is time to emit the signal, you just invoke its emit method,

    if database_has_changed :
        self.databaseChanged.emit()

It looks as if you are trying to have your signal not just exist but pass a
parameter? The syntax for that in pyQtSignal is a bit obscure and I'm not
comfortable with it so maybe somebody else can write about that.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://www.riverbankcomputing.com/pipermail/pyqt/attachments/20150527/5400822b/attachment.html>


More information about the PyQt mailing list