QML TextEdit, validator, regex with \pL — how to do it?
-
Following up on an older discussion:
I need to use Unicode-aware (
\pL
-style) letter validation in a [Edit: multi-line]TextEdit
item, which does not support avalidator
property as QML's single-line text items do, and was surprised to find that the ECMA implementation is too old to recognize Unicode character property escapes in regexes (added in 2018). Because of that, a block of script code in anonTextChanged
handler can't do the same validation that aRegularExpressionValidator
can.(Qt's documentation explains: "Note that QML parses JavaScript regular expressions, while Qt's QRegularExpression class' regular expressions are based on Perl regular expressions." The problem I have is that the JavaScript REs are feature-limited due to QML's pre-2018 ECMA implementation.)
Any suggestions, better than "pick out the bad characters"? I need this code to be fully Unicode-aware and not specific to the letter set of any one language. I would be most grateful for a working example!
Note: I've tried using XRegExp/regex+, but either it's not suited for use in QML or I'm not understanding it properly.
-
Just use a
TextField
instead of aTextEdit
. It supports validators out of the box. -
Just use a
TextField
instead of aTextEdit
. It supports validators out of the box.@Axel-Spoerl That's good for single-line text handling, but in my case (as in the discussion I linked) I require a multi-line text item. I am currently using an invisible dummy
TextField
in the way you describe (see comment #2 above), but, apart from the code ugliness, I have difficulties with things like restoring the previous text selection bounds after rejecting an invalid character, and there is no "fixup" capability that would let me accept a paste while rejecting any included invalid characters. -
QRegularExpression in Qt does support Unicode properties like \p{L}
This means in C++ you can write a filter to allow only Unicode letters.@zvoopz Right, that's what I do in my non-QML widget code. For my QML code, exposing a C++ object class may be the best solution, if only because
QRegularExpressionValidator
can do an intelligent "fixup" that would let me accept a paste while filtering out any invalid characters, but it's odd to me that [a]TextEdit
still doesn't have avalidator
property, and [b] QML is still stuck on ECMA 7th edition, which is now nine years old.