2

When I receive a QLineEdit::textEdited() signal, can I see what the text was prior to the edit? I need to compare the text as it was prior to the edit and the text after the edit.

The textEdited() signal has only one argument, which is the new text. Calling QLineEdit::text() also returns the new text.

Right now I can only think of is holding the old text in a QString, and updating that QString each time there is an edit, but then I need to account for programmatic changes (made via QLineEdit::setText()).

Or is there another way to intercept a text change (via validators perhaps) that would allow me to get the text prior to change and after change simultaneously? Preferably for user-changes only (non programmatic).

László Papp
  • 51,870
  • 39
  • 111
  • 135
sashoalm
  • 75,001
  • 122
  • 434
  • 781
  • If you don't want to cache the old text, I think, using a validator is good alternative. – vahancho Jun 04 '14 at 14:34
  • @vahancho I checked into that, but the `validate()` function gets only the new text, so they won't work, either. I guess I'll have to do the caching. – sashoalm Jun 04 '14 at 14:39
  • Yes, of course, you get the new text, but you get it **before** it is set to the line edit. Thus, if your validator does not accept that, line edit will still contain the old text. – vahancho Jun 04 '14 at 14:41
  • Hm, that's weird, because I had tested it, and calling `lineEdit->text()` from inside `validate()` returns the new text. – sashoalm Jun 04 '14 at 14:54
  • `I need to compare the text as it was prior to the edit and the text after the edit.` -> for what purpose exactly? – László Papp Jun 05 '14 at 01:44
  • @FinalContest I'm trying to implement http://stackoverflow.com/questions/24033222/editable-multi-color-qlineedit, and this is a sub-problem. – sashoalm Jun 05 '14 at 07:10

1 Answers1

1

Right now I can only think of is holding the old text in a QString, and updating that QString each time there is an edit, but then I need to account for programmatic changes (made via QLineEdit::setText())

Granted, I can also only think of that way, but please note that unlike textEdited(), textChanged() would allow you to even catch the programmatic changes as per documentation:

Unlike textChanged(), this signal is not emitted when the text is changed programmatically, for example, by calling setText().

László Papp
  • 51,870
  • 39
  • 111
  • 135