summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorMarcelo Lira <marcelo.lira@openbossa.org>2011-01-06 18:45:44 -0300
committerHugo Parente Lima <hugo.pl@gmail.com>2012-03-08 16:51:44 -0300
commitece91be2e6ab129085d372ce3aac08d4073b42ec (patch)
tree79174267f9a11fce73eeec9f7dd12c7dda155015
parent42e52dec9cd5db837a9fda2663b9c873dac1cd7b (diff)
Fixes connecting signal to decorated slot.
Decorated methods on classes derived from QObject are not called when connected to Qt signals because they get a name different from the decorated method. To solve this decorated methods are registered as global slots. An unit test was added. Reviewed by Hugo Parente <hugo.lima@openbossa.org> Reviewed by Renato Ara├║jo <renato.filho@openbossa.org>
-rw-r--r--PySide/QtCore/glue/qobject_connect.cpp13
-rw-r--r--tests/pysidetest/CMakeLists.txt5
-rw-r--r--tests/pysidetest/decoratedslot_test.py39
3 files changed, 54 insertions, 3 deletions
diff --git a/PySide/QtCore/glue/qobject_connect.cpp b/PySide/QtCore/glue/qobject_connect.cpp
index 91c830ab..f6b0dce0 100644
--- a/PySide/QtCore/glue/qobject_connect.cpp
+++ b/PySide/QtCore/glue/qobject_connect.cpp
@@ -1,9 +1,20 @@
+static bool isDecorator(PyObject* method, PyObject* self)
+{
+ Shiboken::AutoDecRef methodName(PyObject_GetAttrString(method, "__name__"));
+ if (!PyObject_HasAttr(self, methodName))
+ return true;
+ Shiboken::AutoDecRef otherMethod(PyObject_GetAttr(self, methodName));
+ return otherMethod.object() != method;
+}
+
static bool getReceiver(PyObject* callback, QObject** receiver, PyObject** self)
{
+ bool forceGlobalReceiver = false;
if (PyMethod_Check(callback)) {
*self = PyMethod_GET_SELF(callback);
if (Shiboken::Converter<QObject*>::checkType(*self))
*receiver = Shiboken::Converter<QObject*>::toCpp(*self);
+ forceGlobalReceiver = isDecorator(callback, *self);
} else if (PyCFunction_Check(callback)) {
*self = PyCFunction_GET_SELF(callback);
if (*self && Shiboken::Converter<QObject*>::checkType(*self))
@@ -14,7 +25,7 @@ static bool getReceiver(PyObject* callback, QObject** receiver, PyObject** self)
*self = 0;
}
- bool usingGlobalReceiver = !*receiver;
+ bool usingGlobalReceiver = !*receiver || forceGlobalReceiver;
if (usingGlobalReceiver) {
PySide::SignalManager& signalManager = PySide::SignalManager::instance();
*receiver = signalManager.globalReceiver();
diff --git a/tests/pysidetest/CMakeLists.txt b/tests/pysidetest/CMakeLists.txt
index 4b94a2af..61ee424e 100644
--- a/tests/pysidetest/CMakeLists.txt
+++ b/tests/pysidetest/CMakeLists.txt
@@ -72,9 +72,10 @@ target_link_libraries(testbinding
add_dependencies(testbinding pyside QtCore QtGui libpyside pysidetest)
-PYSIDE_TEST(homonymoussignalandmethod_test.py)
+PYSIDE_TEST(decoratedslot_test.py)
PYSIDE_TEST(delegatecreateseditor_test.py)
+PYSIDE_TEST(homonymoussignalandmethod_test.py)
+PYSIDE_TEST(list_signal_test.py)
PYSIDE_TEST(modelview_test.py)
PYSIDE_TEST(version_test.py)
-PYSIDE_TEST(list_signal_test.py)
diff --git a/tests/pysidetest/decoratedslot_test.py b/tests/pysidetest/decoratedslot_test.py
new file mode 100644
index 00000000..63a5be72
--- /dev/null
+++ b/tests/pysidetest/decoratedslot_test.py
@@ -0,0 +1,39 @@
+#!/usr/bin/python
+
+import unittest
+from PySide.QtCore import QObject
+from testbinding import TestObject
+
+class Receiver(QObject):
+ def __init__(self):
+ QObject.__init__(self)
+ self.called = False
+
+ def ReceiverDecorator(func):
+ def decoratedFunction(self, *args, **kw):
+ func(self, *args, **kw)
+ return decoratedFunction
+
+ # This method with the same name of the internal decorated function
+ # is here to test the binding capabilities.
+ def decoratedFunction(self):
+ pass
+
+ @ReceiverDecorator
+ def slot(self):
+ self.called = True
+
+
+class DecoratedSlotTest(unittest.TestCase):
+
+ def testCallingOfDecoratedSlot(self):
+ obj = TestObject(0)
+ receiver = Receiver()
+ obj.staticMethodDouble.connect(receiver.slot)
+ obj.emitStaticMethodDoubleSignal()
+ self.assert_(receiver.called)
+
+
+if __name__ == '__main__':
+ unittest.main()
+