[PyQt] Suggestion: connect overloaded signal based on callable signature (as returned by inspect.signature)

Antony Lee antony.lee at berkeley.edu
Sat Jan 16 07:07:37 GMT 2016


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?

Antony Lee
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://www.riverbankcomputing.com/pipermail/pyqt/attachments/20160115/15a207ac/attachment.html>


More information about the PyQt mailing list