可选信号参数
Optional Signal Arguments
我有一个具有默认关键字参数的函数。我在实现这个时遇到了麻烦,因为我不断收到一个错误,如果我的信号有两个参数,那么我需要传递这两个参数。有什么解决办法吗?
class Controller(QWidget):
trigger = pyqtSignal(str, str)
def __init__(self):
self.trigger.connect(self.myfunc)
@pyqtSlot(str, str)
def function(argument, optional_argument=''):
do something
c = Controller()
c.trigger.emit('Hello', 'World') # This works
c.trigger.emit('Hello') # This fails and says I need 2 arguments
除了重载信号支持的类型外,还必须使连接指向连接中的签名:
import sys
from PyQt5 import QtCore
class Controller(QtCore.QObject):
trigger = QtCore.pyqtSignal([str], [str, str])
def __init__(self):
super(Controller, self).__init__()
self.trigger[str].connect(self.function)
self.trigger[str, str].connect(self.function)
@QtCore.pyqtSlot(str)
@QtCore.pyqtSlot(str, str)
def function(self, argument, optional_argument=''):
print(argument, optional_argument)
def main():
app = QtCore.QCoreApplication(sys.argv)
c = Controller()
c.trigger[str].emit('Hello')
c.trigger[str, str].emit('Hello', 'World')
QtCore.QTimer.singleShot(100, QtCore.QCoreApplication.quit)
sys.exit(app.exec_())
if __name__ == "__main__":
main()
我有一个具有默认关键字参数的函数。我在实现这个时遇到了麻烦,因为我不断收到一个错误,如果我的信号有两个参数,那么我需要传递这两个参数。有什么解决办法吗?
class Controller(QWidget):
trigger = pyqtSignal(str, str)
def __init__(self):
self.trigger.connect(self.myfunc)
@pyqtSlot(str, str)
def function(argument, optional_argument=''):
do something
c = Controller()
c.trigger.emit('Hello', 'World') # This works
c.trigger.emit('Hello') # This fails and says I need 2 arguments
除了重载信号支持的类型外,还必须使连接指向连接中的签名:
import sys
from PyQt5 import QtCore
class Controller(QtCore.QObject):
trigger = QtCore.pyqtSignal([str], [str, str])
def __init__(self):
super(Controller, self).__init__()
self.trigger[str].connect(self.function)
self.trigger[str, str].connect(self.function)
@QtCore.pyqtSlot(str)
@QtCore.pyqtSlot(str, str)
def function(self, argument, optional_argument=''):
print(argument, optional_argument)
def main():
app = QtCore.QCoreApplication(sys.argv)
c = Controller()
c.trigger[str].emit('Hello')
c.trigger[str, str].emit('Hello', 'World')
QtCore.QTimer.singleShot(100, QtCore.QCoreApplication.quit)
sys.exit(app.exec_())
if __name__ == "__main__":
main()