PyQt5 signal connection with a receiver context object?

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


Hi,

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)

https://doc.qt.io/qt-5/qobject.html#connect-5

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
                                   obj2.deleteLater())
    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: <https://www.riverbankcomputing.com/pipermail/pyqt/attachments/20201113/ac3c9b3b/attachment.key>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: OpenPGP_signature
Type: application/pgp-signature
Size: 495 bytes
Desc: OpenPGP digital signature
URL: <https://www.riverbankcomputing.com/pipermail/pyqt/attachments/20201113/ac3c9b3b/attachment.sig>


More information about the PyQt mailing list