将默认参数和自定义参数传递给插槽函数

时间:2018-10-29 08:34:03

标签: python pyqt

我正在PyQt5和Python3.6中使用信号/插槽机制。

我知道如何(在slot函数中)检索链接到发射信号的“默认”参数:

self.myQLineEdit.textEdited.connect(self.my_slot_function)
def my_slot_function(self, text: str) {
    print(text)
}

我还知道如何将自定义参数发送到我的广告位函数:

my_param = 123
self.myQLineEdit.textEdited.connect(lambda: self.my_slot_function(my_param))
def my_slot_function(self, param: int) {
    print(str(param))
}

但是我不知道如何在保留原始“默认”参数的同时发送自定义参数

那会是这样的:

my_param = 123
self.myQLineEdit.textEdited.connect(lambda: self.my_slot_function(default, my_param))
def my_slot_function(self, text: str, param: int) {
    print(text)
    print(str(param))
}

1 个答案:

答案 0 :(得分:1)

尝试一下:

import sys
from PyQt5.QtWidgets import *

class Widget(QWidget):
    def __init__(self, *args, **kwargs):
        QWidget.__init__(self, *args, **kwargs)

        my_param = 123        

        self.myQLineEdit  = QLineEdit()

        self.myQLineEdit.textEdited.connect(
            lambda default, my_param=my_param: self.my_slot_function(default, my_param)
            )

        lay = QVBoxLayout(self)
        lay.addWidget(self.myQLineEdit)

    def my_slot_function(self, text: str, param: int): 
        print(text)
        print(str(param))

if __name__ == '__main__':
    app = QApplication(sys.argv)
    w = Widget()
    w.show()
    sys.exit(app.exec_())

enter image description here

相关问题