PyQt5 signal connection with a receiver context object?

Jeremy Katz jkatz at
Sat Nov 14 07:24:54 GMT 2020


Is there a PyQt equivalent to this version of QObject::connect?

static QMetaObject::Connection QObject::connect(
    const QObject *sender,
    PointerToMemberFunction signal,
    const QObject *context,
    Functor functor,
    Qt::ConnectionType type)

The appeal of this version for a lambda slot is:
* automatic disconnection if sender or context are destroyed
* slot invocation according to context's thread and the connection type

My use case is connecting to lambda slots that capture a QObject.

For example:
    obj1 = QObject()
    obj2 = QObject()
    obj1.destroyed.connect(lambda: print("scheduling obj2 deletion") or
    del obj2
    del obj1
    scheduling obj2 deletion
    Traceback (most recent call last):
    File "<stdin>", line 2, in <lambda>
    NameError: name 'obj2' is not defined
    Abort trap: 6
-------------- next part --------------
A non-text attachment was scrubbed...
Name: OpenPGP_0x095F4F2148BF111D.asc
Type: application/pgp-keys
Size: 1757 bytes
Desc: not available
URL: <>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: OpenPGP_signature
Type: application/pgp-signature
Size: 495 bytes
Desc: OpenPGP digital signature
URL: <>

More information about the PyQt mailing list