aboutsummaryrefslogtreecommitdiffstats
path: root/sources/pyside2/libpyside
diff options
context:
space:
mode:
authorCristian Maureira-Fredes <cristian.maureira-fredes@qt.io>2018-02-13 14:45:46 +0100
committerCristian Maureira-Fredes <cristian.maureira-fredes@qt.io>2018-03-02 16:00:23 +0000
commit095fd339a238fe2ad5ec6080119882436d95fde3 (patch)
tree2cf8db3a692b4a622194168282d0ef2ce3f996b2 /sources/pyside2/libpyside
parente9e157032ab383c894c38517a047860232e45482 (diff)
Check slot-signal association when connecting
When connecting a signal with a slot there is a process to associate the proper signal signature, but the slot signature was not verified. This missing verification step lead to wrongly associate the slots and the signal signatures, for example: def on_clicked(checked=True): ... QGroupBox.clicked.connect(on_clicked) will wrongly connect the slot "on_clicked" with the signal "clicked()" (without any argument), when the proper signal is "clicked(bool)". This can be solved by manually specifying the arguments: QGroupBox.clicked[bool].connect(self.clicked) We can add an additional verification step to associate the proper signal if the slot has a certain number of arguments. There is an existing test that checks the compatibility of this change with all the ways to connect signals and slots. A few additional cases were added. Task-number: PYSIDE-104 Change-Id: Ic5b06fa3bb91903f7d506e0e2c52a6f7d3dc4570 Reviewed-by: Christian Tismer <tismer@stackless.com>
Diffstat (limited to 'sources/pyside2/libpyside')
-rw-r--r--sources/pyside2/libpyside/pysidesignal.cpp59
1 files changed, 56 insertions, 3 deletions
diff --git a/sources/pyside2/libpyside/pysidesignal.cpp b/sources/pyside2/libpyside/pysidesignal.cpp
index 04b1cf1f4..a901d10af 100644
--- a/sources/pyside2/libpyside/pysidesignal.cpp
+++ b/sources/pyside2/libpyside/pysidesignal.cpp
@@ -413,11 +413,64 @@ PyObject* signalInstanceConnect(PyObject* self, PyObject* args, PyObject* kwds)
sourceWalk = reinterpret_cast<PySideSignalInstance*>(sourceWalk->d->next);
}
} else {
- //try the first signature
+ // Check signature of the slot (method or function) to match signal
+ int slotArgs = -1;
+ bool useSelf = false;
+ bool isMethod = PyMethod_Check(slot);
+ bool isFunction = PyFunction_Check(slot);
+ bool matchedSlot = false;
+
+ QByteArray functionName;
+ PySideSignalInstance *it = source;
+
+ if (isMethod || isFunction) {
+ PyObject *function = isMethod ? PyMethod_GET_FUNCTION(slot) : slot;
+ PyCodeObject *objCode = reinterpret_cast<PyCodeObject *>(PyFunction_GET_CODE(function));
+ PyFunctionObject *function_obj = reinterpret_cast<PyFunctionObject *>(function);
+ functionName = Shiboken::String::toCString(function_obj->func_name);
+ useSelf = isMethod;
+ slotArgs = objCode->co_flags & CO_VARARGS ? -1 : objCode->co_argcount;
+ if (useSelf)
+ slotArgs -= 1;
+
+ // Get signature args
+ bool isShortCircuit = false;
+ int signatureArgs = 0;
+ QStringList argsSignature;
+
+ argsSignature = PySide::Signal::getArgsFromSignature(it->d->signature,
+ &isShortCircuit);
+ signatureArgs = argsSignature.length();
+
+ // Iterate the possible types of connection for this signal and compare
+ // it with slot arguments
+ if (signatureArgs != slotArgs) {
+ while (it->d->next != nullptr) {
+ it = it->d->next;
+ argsSignature = PySide::Signal::getArgsFromSignature(it->d->signature,
+ &isShortCircuit);
+ signatureArgs = argsSignature.length();
+ if (signatureArgs == slotArgs) {
+ matchedSlot = true;
+ break;
+ }
+ }
+ }
+ }
+
+ // Adding references to pyArgs
PyList_Append(pyArgs, source->d->source);
- Shiboken::AutoDecRef signature(PySide::Signal::buildQtCompatible(source->d->signature));
- PyList_Append(pyArgs, signature);
+ if (matchedSlot) {
+ // If a slot matching the same number of arguments was found,
+ // include signature to the pyArgs
+ Shiboken::AutoDecRef signature(PySide::Signal::buildQtCompatible(it->d->signature));
+ PyList_Append(pyArgs, signature);
+ } else {
+ // Try the first by default if the slot was not found
+ Shiboken::AutoDecRef signature(PySide::Signal::buildQtCompatible(source->d->signature));
+ PyList_Append(pyArgs, signature);
+ }
PyList_Append(pyArgs, slot);
match = true;
}