[Pyside2] Multiple QLineEdit signals not working
-
Hi !
Sorry in advance for my english, it's not my main language.I try to have this code : objQLineEdit.signal.connect(self.function) working under a for loop.
For testing, i just use a simple print.For example with this :
for i in range(10):
objQLineEdit = QLineEdit()
objQLineEdit.signal.connect(lambda: self.function(objQLineEdit.text()))
<END CODE>Only the last QLineEdit has a signal that is working.
How can i get all of them work ?Thank's in advance.
-
@Geoffrey said in [Pyside2] Multiple QLineEdit signals not working:
Only the last QLineEdit has a signal that is working.
I do not believe that is so. Replace by:
objQLineEdit.signal.connect(lambda: print("Some signal"))
(I don't know what your particular
objQLineEdit.signal
is, I presume you have something likeclicked
there, not actuallysignal
. I assume your whole code is an "outline", not real code.)Does that indeed print when any of the edits are pressed? I believe it should.
What you may have a problem with in Python is your particular lambda:
objQLineEdit.signal.connect(lambda: self.function(objQLineEdit.text()))
I'm not 100% about this, but I think that will only ever pass the last value of
objQLineEdit
. Try:objQLineEdit.signal.connect(lambda lineEdit=objQLineEdit: self.function(lineEdit.text()))
If that works, this is a Python-esque issue.
-
@Geoffrey
This business of needing in Python to write:for objQLineEdit in ...: objQLineEdit.signal.connect(lambda lineEdit=objQLineEdit: self.function(lineEdit.text()))
really threw me when I first came across it. It works differently from the C++ lambda:
connect(objQLineEdit, &Class::signal, [objQLineEdit]() { qDebug() << objLineEdit.text(); });
In the C++ you get the lambda's
objLineEdit
to be the current value ofobjQLineEdit
when it was set up, like you'd expect.But in the Python
objQLineEdit.signal.connect(lambda: self.function(objQLineEdit.text()))
you get all the lambdas connecting to the last value
objQLineEdit
had when the loop was run. Passing it as a parameter to the lambda byobjQLineEdit.signal.connect(lambda lineEdit=objQLineEdit: self.function(lineEdit.text()))
seems to be the equivalent of the C++ lambda, so that it references the
objQLineEdit
as it was when the line was executed.Keep this in mind, as it comes up again whenever you write Python lambdas in Qt! :)
-
@JonB said in [Pyside2] Multiple QLineEdit signals not working:
@Geoffrey
This business of needing in Python to write:for objQLineEdit in ...: objQLineEdit.signal.connect(lambda lineEdit=objQLineEdit: self.function(lineEdit.text()))
really threw me when I first came across it.
I have the same reaction :p
Keep this in mind, as it comes up again whenever you write Python lambdas in Qt! :)
Yes i try to not reproduce twice the same mistake and learn from it :)