1

I'm using QtQuick 1.0 and I'd like to have TextInput element with input to get only digits and ".". To obtain only digits I use the following code :

TextInput {
    id: textInput

    anchors.centerIn : inputArea
    font.family : "Helvetica"
    font.pixelSize: textSize
    color: "black"
    maximumLength: 5
    smooth: true
    inputMask: "99999"
    readOnly: isReadOnly
}

And I'm able to input only digits. How should I extend it to get "." also ?

3 Answers 3

6

After several runs I came to the following solution :

TextInput {
    id: textInput

    anchors.centerIn : inputArea
    font.family : "Helvetica"
    font.pixelSize: textSize
    color: "black"
    maximumLength: 5
    smooth: true
    validator : RegExpValidator { regExp : /[0-9]+\.[0-9]+/ }
    readOnly: isReadOnly
}
Sign up to request clarification or add additional context in comments.

Comments

0

You could also use inputMethodHints:

TextInput {
    id: textInput
    inputMethodHints: Qt.ImhFormattedNumbersOnly
    ...
}

Comments

0

While you can use a RegExpValidator, the proper way to handle such situations is probably to use DoubleValidator directly.

See the documentation for more details: it also provides some feature useful when working with floating point numbers.

TextInput {
    id: textInput

    anchors.centerIn : inputArea
    font.family : "Helvetica"
    font.pixelSize: textSize
    color: "black"
    smooth: true
    validator : DoubleValidator {
      decimals: 2
      top: 99999
      bottom: -99999
    }
    readOnly: isReadOnly
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.