TextFields with masks or validators seem unusable
-
Maybe I'm missing something: If you use a TextField's inputMask property or a validator, I don't see how you can use it; because the onEditingFinished signal never fires.
Also, invalid text remains in the control when it loses focus. Because no signal fires at that point, your program can't even refresh the text.
I have a TextField with a mask of "Nnnnnnnnn". It should require one alphanumeric character and allow up to eight more. But the user can enter all blanks (or nothing) and that's how the content stays after the control loses focus.
So... how is this usable?
-
@Stokestack thats the point, you only get an editingFinished signal, when the user has entered a valid input, but there is no build in way to reset the text to its original value, when the input looses focus and an invalid input was made. You would have to store that value and reset it, when the TextField looses focus.
you can somewhat restrict the user input, by setting an input mask and an input validator at the same time.
-
@J-Hilk said in TextFields with masks or validators seem unusable:
you would have to store that value and reset it, when the TextField looses focus.
But there's no (documented) signal to tell you when it loses focus. That's a big part of the problem.
And what if the user presses Enter or Escape? Then they're "done editing," but the control presumably still has focus.
-
@Stokestack said in TextFields with masks or validators seem unusable:
But there's no (documented) signal to tell you when it loses focus. That's a big part of the problem.
focus is a property it comes with the
onFocusChanged
signal....But, I totally agree, since there's an accepted signal, there should be a rejected signal as well, would make the world much easier
-
For one of our applications we have a Text Item that displays the value that is current. When the activeFocus is acquired by our TextInput we hide the Text Item and show the TextInput. When value is accepted we update the variable for the Text Item and hide the TextInput. This just happens to work for us. So we don't care if crap was in the TextInput as it won't affect the actual value if it is not accepted.
-
@fcarney This can be done with a single TextField if I understand what you are saying:
Column { anchors.centerIn: parent Label { id: backend text: "Initial Text" } TextField { id: textField Binding on text { when: !textField.activeFocus value: backend.text restoreMode: Binding.RestoreNone } onAccepted: backend.text = text Keys.onEscapePressed: textField.focus = false onActiveFocusChanged: { if (activeFocus) selectAll(); } background.visible: activeFocus selectByMouse: true } }
I'm using something like this in one of our app, but by using
readOnly
instead ofactiveFocus
and some buttons to change thereadOnly
state.Here is a video of it in action: https://streamable.com/h2aof5
-
Thanks for the replies, guys. I'm pretty new to QML, so the binding example is very helpful.
I still think these text controls desperately need improvement, though.