[PyQt] Suggestion: connect overloaded signal based on callable signature (as returned by inspect.signature)
Phil Thompson
phil at riverbankcomputing.com
Sun Jan 24 18:33:30 GMT 2016
On 16 Jan 2016, at 7:07 am, Antony Lee <antony.lee at berkeley.edu> wrote:
>
> Consider the following example:
>
> from functools import wraps
> from PyQt4.QtCore import *
> from PyQt4.QtGui import *
>
> def decorator(func):
> @wraps(func)
> def wrapper(*args, **kwargs):
> return func(*args, **kwargs)
> return wrapper
>
> class C(QComboBox):
> @decorator
> def method(self):
> print("here")
>
> app = QApplication([])
> c = C()
> c.activated.connect(c.method)
> c.activated.emit(1)
>
> This fails (also under PyQt5) with "TypeError: method() takes 1 positional argument but 2 were given", i.e. the connection was made using the "activated[int]" signal overload. But the actual signature of "c.method" (as taking no argument) is in fact available (since Python 3.3) via "inspect.signature".
>
> I am thus suggesting to rely on "inspect.signature" for the overload resolution. (I am aware that decorating with pyqtSlot() can also resolve the ambiguity.)
>
> Thoughts?
At this stage it would be a lot of effort (to parse the signature and translate it into an equivalent C++ version) for little benefit.
Had this been available when pyqtSlot() was invented, then it might have been worth it.
Phil
More information about the PyQt
mailing list