From 095fd339a238fe2ad5ec6080119882436d95fde3 Mon Sep 17 00:00:00 2001 From: Cristian Maureira-Fredes Date: Tue, 13 Feb 2018 14:45:46 +0100 Subject: 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 --- .../pyside2/tests/signals/signal_signature_test.py | 40 +++++++++++++++++++++- 1 file changed, 39 insertions(+), 1 deletion(-) (limited to 'sources/pyside2/tests') diff --git a/sources/pyside2/tests/signals/signal_signature_test.py b/sources/pyside2/tests/signals/signal_signature_test.py index 349619aac..e94c1722d 100644 --- a/sources/pyside2/tests/signals/signal_signature_test.py +++ b/sources/pyside2/tests/signals/signal_signature_test.py @@ -34,7 +34,11 @@ import unittest from PySide2.QtCore import * from helper import UsesQCoreApplication +called = False +name = "Old" class Obj(QObject): + dummySignalArgs = Signal(str) + numberSignal = Signal(int) def __init__(self): QObject.__init__(self) self.signal = '' @@ -42,8 +46,20 @@ class Obj(QObject): def connectNotify(self, signal): self.signal = signal + @staticmethod + def static_method(): + global called + called = True + + @staticmethod + def static_method_args(arg="default"): + global name + name = arg + def callback(arg=None): pass +def callback_empty(): + pass class TestConnectNotifyWithNewStyleSignals(UsesQCoreApplication): '''Test case for signal signature received by QObject::connectNotify().''' @@ -65,12 +81,34 @@ class TestConnectNotifyWithNewStyleSignals(UsesQCoreApplication): def testNewStyle(self): sender = Obj() - sender.destroyed.connect(callback) + sender.destroyed.connect(callback_empty) self.assertEqual(sender.signal.methodSignature(), 'destroyed()') sender.destroyed[QObject].connect(callback) self.assertEqual(sender.signal.methodSignature(), 'destroyed(QObject*)') + def testStaticSlot(self): + global called + sender = Obj() + sender.connect(sender, SIGNAL("dummySignal()"), Obj.static_method) + sender.emit(SIGNAL("dummySignal()")) + self.assertTrue(called) + + + def testStaticSlotArgs(self): + global name + sender = Obj() + sender.dummySignalArgs.connect(Obj.static_method_args) + sender.dummySignalArgs[str].emit("New") + self.assertEqual(name, "New") + + def testLambdaSlot(self): + sender = Obj() + sender.numberSignal[int].connect(lambda x: 42) + with self.assertRaises(IndexError): + sender.numberSignal[str].emit("test") + + if __name__ == '__main__': unittest.main() -- cgit v1.2.3