0

I am using signal/slot mechanism with PyQt5 and Python3.6.

I know how to retrieve (in the slot function) the "default" argument linked to the emitted signal:

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

I also know how to send a custom argument to my slot function:

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

But I don't know how to send a custom argument while keeping the original "default" argument.

It would be something like:

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))
}
Blacksad
  • 1,230
  • 1
  • 14
  • 23
  • The answer of the "duplicated" question fits with my needs, but the "duplicated" question itself is quite unclear, and I was not able to find it. – Blacksad Oct 29 '18 at 09:54

1 Answers1

1

Try it:

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

S. Nick
  • 12,879
  • 8
  • 25
  • 33