1

so I'm currently working on a PyQt5 GUI, and as always need to connect some signals to method calls. Naturally I've looked up a standard syntax to do so and used it throughout my entire project (it's been working so far with more then 20 different signals) That syntax is: self.widget.signal.connect(lambda x: whatever)

So I recently got to the point of connecting the QPlainTextEdit signal "textChanged()" to one of my methods and it just didn't work. I've tried replacing my method with a simple print(text) but that didn't help. My next step was testing wether another signal of the same widget worked and it did! So now I have the following code:

self.plainTextEdit.textChanged.connect(lambda x: print("testTextChanged"))
self.plainTextEdit.blockCountChanged.connect(lambda x: print("blockCountChanged"))

and the upper signal doesn't trigger, but the lower one does.

I've already read the documentation of QPlainTextEdit, textChanged() should be a valid signal of this class. I've also used the same signal on several QLineEdits within my project.

Does anyone have any suspicion as to why this behaviour is occuring? Maybe I did make an error that I just can't recognize. (I'm trying to trigger the signal by simply typing into the textBox on the GUI, whereas blockCountChanged get's triggered whenever I'm pressing enter while editing it)

  • 1
    [`textChanged()`](https://doc.qt.io/qt-5/qplaintextedit.html#textChanged) doesn't have any argument, so the lambda shouldn't have any as well. In any case, please provide a [minimal, reproducible example](https://stackoverflow.com/help/minimal-reproducible-example). – musicamante Dec 04 '20 at 13:31
  • Oh wow, I did not recognize that... but thanks for the help, it worked without the x! And next time I will ;) – TheBest_Kappa Dec 04 '20 at 13:46

1 Answers1

2

So, the comment of musicamente (comment on the question) did answer it. The reason it did not work is, because the textChanged signal of QPlainTextEdit does not have any parameters (QLineEdit textChanged does f.e.). That's why the lambda should not have and parameters -> the correct code should be:

self.plainTextEdit.textChanged.connect(lambda: print("testTextChanged"))

PS: just answering this if someone searches for the same stuff.