Important: Please read the Qt Code of Conduct -

Numeric virtual keyboard for QML TextInput

  • Hi all,
    I have a QML TextInput element that should hold a TCP port number, so the user should only enter numbers in that TextInput; Therefore I would like the virtual keyboard that opens when clicking on the TextInput to contain only digits (you know, like the numeric virtual keyboard on iPhone). I tried adding this (just like in the documentation):
    @ TextInput{
    validator: IntValidator{bottom: 11; top: 31;}
    focus: true
    However the virtual keyboard that opens is qwerty keyboard, I am able to input literal characters because auto-correction is enabled (btw does anyone know how to disable autocorrection??), but when pressing the return key or accepting the autocorrection suggestion the TextInput text disappears, because the text entered was not a number between 11 and 31 like in the code above. However this is so confusing for the end user ! It would be so MUCH SIMPLER if the virtual keyboard would have only digits.

    How can I do this? (Again this is so easy on iPhone..., both as a user and a programmer, no offense).

  • I think that what you do is correct. As a matter of fact the validator should check what you input in the field while the focus opens the keyboard. It has no matter that the keyboard is opened in one configuration or another. This is with this code a user decision. Simply if the user try to insert and alphanumeric character it is not accepet because the validator.

    What should be seen is if there is a way to filter the focus action: when the object gain focus you should intercept this event and do what you need - if possible - i.e. showing the keyboard in the numeric mode instead of the standard VKB mode.
    As you can see in this "focus documentation": controls on the key pressed are done on the text input object content. This means that the user should have pressed something. I have not found anything related to a specific keyboard setting in some condition.

    Anyway there are some projects in that manages custom keyboards. I think this maybe somehting that help you in this problem.

  • There is a simpler solution, if you add the "inputMethodHints" property you can select what type of input you wish to have for the TextInput eg:

    inputMethodHints: Qt.ImhFormattedNumbersOnly

    There are a variety of hints that can be used, the documentation for QML doesn't really explain that these are usable. You have to look at the "C++ documentation": to find out what is available.

    Remember to substitute the namespace operator "::" with '.' to use the values with QML.

Log in to reply