Validator for TextArea/TextEdit
-
I am writing a hex editor and would like a validator on a QML TextEdit/TextArea component in order to disallow any non-hex characters. Validators seem to be supported in the single-line components TextInput/TextField, but those are sadly not appropriate for an editor.
Can this be done?
-
Hi
my first feeling in this case would be monitoring any text change, remove any prohibited character if any (would be located just before the current cursor position), then reposition the cursor if necessary.
something like this
TextEdit // Or TextArea { id: textEdit anchors, height or width at your convenience onTextChanged: { const oldCursorPosition = cursorPosition text = textParserFunction (text) cursorPosition = oldCursorPosition - 1 } }
-
@ankou29666 That's my thinking as well, although IMHO that's an ugly hack to work around the lack of a validator. Maybe that's the best we can do, though?
-
I don't really like this either but this is the same kind of bullshit I use to prevent the cursor to be freely moving (I mean forcing the cursor back behind the last valid character) when using an input mask over a TextEdit
-
@ankou29666 I'm guessing you have to have some guards on
text
so that the self-modification oftext
doesn't trigger an infinite loop ofonTextChange()
events? -
@Kenz-Dale In normal world of Qt, if you set a property which is already set to the not so new value, the propertyChanged signal is not emitted.
-
@ankou29666 What if you capture key presses and only let what you want get through to the TextEdit? event.accepted = true only for keys you want to be pressed.
Edit: I mean false. Also, I think I meant to reply to @Kenz-Dale