From ece91be2e6ab129085d372ce3aac08d4073b42ec Mon Sep 17 00:00:00 2001 From: Marcelo Lira Date: Thu, 6 Jan 2011 18:45:44 -0300 Subject: Fixes connecting signal to decorated slot. MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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 Reviewed by Renato Araújo --- PySide/QtCore/glue/qobject_connect.cpp | 13 +++++++++++- tests/pysidetest/CMakeLists.txt | 5 +++-- tests/pysidetest/decoratedslot_test.py | 39 ++++++++++++++++++++++++++++++++++ 3 files changed, 54 insertions(+), 3 deletions(-) create mode 100644 tests/pysidetest/decoratedslot_test.py diff --git a/PySide/QtCore/glue/qobject_connect.cpp b/PySide/QtCore/glue/qobject_connect.cpp index 91c830abd..f6b0dce08 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::checkType(*self)) *receiver = Shiboken::Converter::toCpp(*self); + forceGlobalReceiver = isDecorator(callback, *self); } else if (PyCFunction_Check(callback)) { *self = PyCFunction_GET_SELF(callback); if (*self && Shiboken::Converter::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 4b94a2aff..61ee424e4 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 000000000..63a5be72a --- /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() + -- cgit v1.2.3