PDA

View Full Version : validate QPlainTextEdit text



adeas31
2nd March 2016, 16:13
I want to validate QPlainTextEdit text whenever user clicks on somewhere else in the GUI e.g., toolbar or menubar. QFocusOutEvent is not helpful because lets say I click on toolbar button then first the slot connected to toolbar button is activated and then QFocusOutEvent is triggered. How do I make sure I get it first so I can validate and then proceed with toolbar action.

Adeel.

d_stranz
2nd March 2016, 16:42
You can connect a slot to the QPlainTextEdit::textChanged() signal or to the QTextDocument::contentsChanged() signal. These might be emitted on a character-by-character basis, but they look like the only way you can do validation. Unless you disable the rest of the GUI when the content is not valid, you can't really prevent the user from clicking elsewhere.

adeas31
2nd March 2016, 16:47
As you already said "these might be emitted on a character-by-character basis" so this means I am most of the time reporting error to users because the text will be incomplete and of course invalid.

d_stranz
2nd March 2016, 16:51
text will be incomplete and of course invalid

In which case your validation needs to recognize that partial text is also valid. You might be able to use QValidator for this because it does have a model for "intermediate" validation.

anda_skoa
3rd March 2016, 10:50
Another option would be to not assume that the content is valid when you perform an action on it, but to explicitly verify it.

E.g. if you have a toolbar action that copies the content into the clipboard, make it first check if that is allowed.

Like doing a final check in a dialog when OK is clicked, or on a web form when it is submitted.

Cheers,
_