【问题标题】:Passing default argument AND custom argument to slot function [duplicate]将默认参数和自定义参数传递给槽函数[重复]
【发布时间】:2019-04-02 03:37:42
【问题描述】:

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

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

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))
}

【问题讨论】:

  • “重复”问题的答案符合我的需求,但是“重复”问题本身就很不清楚,我没能找到。

标签: python pyqt


【解决方案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_())

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-04-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多