{"id":116,"date":"2010-09-09T16:36:00","date_gmt":"2010-09-09T16:36:00","guid":{"rendered":"https:\/\/www.root42.de\/blog\/?p=116"},"modified":"2010-09-09T16:36:00","modified_gmt":"2010-09-09T16:36:00","slug":"selecting-between-overloaded-signals-in-pyqt","status":"publish","type":"post","link":"https:\/\/www.root42.de\/blog\/?p=116","title":{"rendered":"Selecting between overloaded signals in PyQt"},"content":{"rendered":"<p>The signal mechanism in <a href=\"http:\/\/qt.nokia.com\/products\/\">Qt<\/a>&nbsp;allows for overloading. E.g. the QSpinBox comes with two signals called <span style=\"font-family: 'Courier New', Courier, monospace;\">valueChanged(int)<\/span> and <span style=\"font-family: 'Courier New', Courier, monospace;\">valueChanged(QString)<\/span>. 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:<\/p>\n<div>\n<pre>from PyQt4 import QtCore, QtGui, uic<br \/><br \/>class MyMainWindow(QtGui.QMainWindow):<br \/><br \/>    def __init__(self):<br \/>        QtGui.QMainWindow.__init__(self)<br \/>        self.ui = uic.loadUi(\"MyMainWindow.ui\", self)<br \/><br \/>        # Slot connections<br \/>        self.ui.spinBox.valueChanged.connect(self.spinBoxChanged)<br \/>        self.ui.spinBox.setValue(4)<br \/><br \/>    @QtCore.pyqtSlot(int)<br \/>    def spinBoxChanged(self, i):<br \/>        # do something...<br \/>        pass<br \/><\/pre>\n<\/div>\n<p>Notice the <span style=\"font-family: 'Courier New', Courier, monospace;\">(int)<\/span> in the decorator of the slot. This has to match the signature of the signal. I haven&#8217;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&#8217;t be clashes with pure Python signals and slots.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>The signal mechanism in Qt&nbsp;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, uicclass MyMainWindow(QtGui.QMainWindow): def __init__(self): QtGui.QMainWindow.__init__(self) &hellip; <a href=\"https:\/\/www.root42.de\/blog\/?p=116\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;Selecting between overloaded signals in PyQt&#8221;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[7,15],"tags":[],"_links":{"self":[{"href":"https:\/\/www.root42.de\/blog\/index.php?rest_route=\/wp\/v2\/posts\/116"}],"collection":[{"href":"https:\/\/www.root42.de\/blog\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.root42.de\/blog\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.root42.de\/blog\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.root42.de\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=116"}],"version-history":[{"count":0,"href":"https:\/\/www.root42.de\/blog\/index.php?rest_route=\/wp\/v2\/posts\/116\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.root42.de\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=116"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.root42.de\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=116"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.root42.de\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=116"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}