Selecting between overloaded signals in PyQt

The signal mechanism in Qt allows for overloading. E.g. the QSpinBox comes with two signals called valueChanged(int) and valueChanged(QString). However, since Python is dynamically typed, such overloading gives rise to problems. Here is how you can select which signal you want to connect to your slot:
from PyQt4 import QtCore, QtGui, uic

class MyMainWindow(QtGui.QMainWindow):

    def __init__(self):
        self.ui = uic.loadUi("MyMainWindow.ui", self)

        # Slot connections

    def spinBoxChanged(self, i):
        # do something...
Notice the (int) in the decorator of the slot. This has to match the signature of the signal. I haven't dug into more complex signals, passing around arbitrary objects, but I think that is not a problem: Qt datatypes have a Python wrapper, and functions in Python cannot be overloaded anyway, so there won't be clashes with pure Python signals and slots.

No comments:

Post a Comment