diff options
author | Friedemann Kleint <Friedemann.Kleint@qt.io> | 2022-12-08 10:12:40 +0100 |
---|---|---|
committer | Friedemann Kleint <Friedemann.Kleint@qt.io> | 2022-12-12 14:25:19 +0100 |
commit | 5b5cb391ccc318128946c64154a3284ae434bf8a (patch) | |
tree | d38160374de34be9ca074756c7b9c8c13496fe6b /sources/pyside6/libpyside/globalreceiverv2.cpp | |
parent | 15ccf2484e605a9188fc0b00a5b0ee59197f2734 (diff) |
Fix QObject::sender() for non-C++ slots
For non-C++ slots routed via GlobalReceiverV2, sender()
of the receiving QObject returns 0. To fix this, store
the sender obtained in GlobalReceiverV2::qt_metacall()
temporarily in a special dynamic property of the receiver
and inject code checking it into QObject::sender(). This
fixes at least the synchronous calls.
Fixes: PYSIDE-2144
Fixes: PYSIDE-1295
Change-Id: Ia111162eb1404914ecfb7f19fadb8a1b63ae8b4a
Reviewed-by: Qt CI Bot <qt_ci_bot@qt-project.org>
Reviewed-by: Cristian Maureira-Fredes <cristian.maureira-fredes@qt.io>
Diffstat (limited to 'sources/pyside6/libpyside/globalreceiverv2.cpp')
-rw-r--r-- | sources/pyside6/libpyside/globalreceiverv2.cpp | 14 |
1 files changed, 12 insertions, 2 deletions
diff --git a/sources/pyside6/libpyside/globalreceiverv2.cpp b/sources/pyside6/libpyside/globalreceiverv2.cpp index 1342af24e..a72455289 100644 --- a/sources/pyside6/libpyside/globalreceiverv2.cpp +++ b/sources/pyside6/libpyside/globalreceiverv2.cpp @@ -155,9 +155,12 @@ DynamicSlotDataV2::~DynamicSlotDataV2() Py_DECREF(m_callback); } -GlobalReceiverV2::GlobalReceiverV2(PyObject *callback) : +const char *GlobalReceiverV2::senderDynamicProperty = "_q_pyside_sender"; + +GlobalReceiverV2::GlobalReceiverV2(PyObject *callback, QObject *receiver) : QObject(nullptr), - m_metaObject("__GlobalReceiver__", &QObject::staticMetaObject) + m_metaObject("__GlobalReceiver__", &QObject::staticMetaObject), + m_receiver(receiver) { m_data = new DynamicSlotDataV2(callback, this); } @@ -246,10 +249,17 @@ int GlobalReceiverV2::qt_metacall(QMetaObject::Call call, int id, void **args) return -1; } + const bool setSenderDynamicProperty = !m_receiver.isNull(); + if (setSenderDynamicProperty) + m_receiver->setProperty(senderDynamicProperty, QVariant::fromValue(sender())); + const bool isShortCuit = std::strchr(slot.methodSignature(), '(') == nullptr; Shiboken::AutoDecRef callback(m_data->callback()); SignalManager::callPythonMetaMethod(slot, args, callback, isShortCuit); + if (setSenderDynamicProperty) + m_receiver->setProperty(senderDynamicProperty, QVariant{}); + // SignalManager::callPythonMetaMethod might have failed, in that case we have to print the // error so it considered "handled". if (PyErr_Occurred()) { |