简体   繁体   中英

PyQt Overloading Pre-Existing Signals

I'm wanting to add a new overload to a pre-existing signal. Here is a very simple example of the concept:

import sys
from PyQt4 import QtGui, QtCore

class MyComboBox(QtGui.QComboBox):
    currentIndexChanged = QtCore.pyqtSignal(float)
    def __init__(self, *args, **kwargs):
        super(MyComboBox, self).__init__(*args, **kwargs)
        self.currentIndexChanged[int].connect(self._on_current_changed)

    def _on_current_changed(self, index):
        self.currentIndexChanged[float].emit(float(index))

def log(value):
    print 'value:', value

app = QtGui.QApplication(sys.argv)
combo = MyComboBox()
combo.addItems(['foo', 'bar', 'baz'])
combo.currentIndexChanged[float].connect(log)
combo.show()
sys.exit(app.exec_())

When I run this I get:

    self.currentIndexChanged[int].connect(self._on_current_changed)
KeyError: 'there is no matching overloaded signal'

My guess is that defining the new signal to have to same name completely overwrites the existing signals but I have no idea how to prevent that.

Overloading a signal is no different than overloading a method. If you want to access the base-class signal, you can do it via super :

super(MyComboBox, self).currentIndexChanged[int].connect(
        self._on_current_changed)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM