Take the 2-minute tour ×
Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free, no registration required.

I need the user to be able to input an integer, restricted to values between 0 and an upper bound. The upper bound is too big to fit within the 'int' data type so I have used a 'real' data type with a DoubleValidator.

The DoubleValidator doesn't properly restrict the upper bound whereas an IntValidator with an int value works as expected.

IntValidator example

This restricts the entered text to integer values between 0 and 1000:

import QtQuick 2.0
import Ubuntu.Components 0.1

MainView {
    id: main
    width: 500
    height: 500

    TextField {
        id: tf
        text: "0"
        anchors.centerIn: parent
        validator: IntValidator {
            bottom: 0
            top: 1000
        }
    }

    property int value: 0+tf.text

    Label {
        id: label
        text: ""+main.value
    }

}

IntValidator screenshot

DoubleValidator example

This restricts the entered text to integer values (doubles with 0 decimal places) and ensures the value is greater than or equal to 0. However, values exceeding 1e10 are allowed - contrary to what would be expected.

import QtQuick 2.0
import Ubuntu.Components 0.1

MainView {
    id: main
    width: 500
    height: 500

    TextField {
        id: tf
        text: "0"
        anchors.centerIn: parent
        validator: DoubleValidator {
            bottom: 0
            top: 1e10
            decimals: 0
        }
    }

    property real value: 0+tf.text

    Label {
        id: label
        text: ""+main.value
    }

}

DoubleValidator screenshot


Why does this not work as expected?

How can long integer input be validated?

share|improve this question
add comment

1 Answer 1

up vote 1 down vote accepted

Adding notation: DoubleValidator.StandardNotation to the DoubleValidator fixes the issue. From its documentation:

If notation is set to DoubleValidator.StandardNotation, and the input contains more digits before the decimal point than a double in the valid range may have, it is also rejected. If notation is DoubleValidator.ScientificNotation, and the input is not in the valid range, it is accecpted but invalid. The value may yet become valid by changing the exponent.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.