This element provides a validator for integer values.
- IntValidator uses the \l {QLocale::setDefault()}{default locale} to interpret the number and
- will accept locale specific digits, group separators, and positive and negative signs. In
- addition, IntValidator is always guaranteed to accept a number formatted according to the "C"
- locale.
+ If no \l locale is set IntValidator uses the \l {QLocale::setDefault()}{default locale} to
+ interpret the number and will accept locale specific digits, group separators, and positive
+ and negative signs. In addition, IntValidator is always guaranteed to accept a number
+ formatted according to the "C" locale.
*/
+
+
+QQuickIntValidator::QQuickIntValidator(QObject *parent)
+ : QIntValidator(parent)
+{
+}
+
+/*!
+ \qmlproperty string QtQuick2::IntValidator::locale
+
+ This property holds the name of the locale used to interpret the number.
+
+ \sa QML:Qt::locale()
+*/
+
+QString QQuickIntValidator::localeName() const
+{
+ return locale().name();
+}
+
+void QQuickIntValidator::setLocaleName(const QString &name)
+{
+ if (locale().name() != name) {
+ setLocale(QLocale(name));
+ emit localeNameChanged();
+ }
+}
+
+void QQuickIntValidator::resetLocaleName()
+{
+ QLocale defaultLocale;
+ if (locale() != defaultLocale) {
+ setLocale(defaultLocale);
+ emit localeNameChanged();
+ }
+}
+
/*!
\qmlproperty int QtQuick2::IntValidator::top
value may yet become valid by changing the exponent.
*/
+QQuickDoubleValidator::QQuickDoubleValidator(QObject *parent)
+ : QDoubleValidator(parent)
+{
+}
+
+/*!
+ \qmlproperty string QtQuick2::DoubleValidator::locale
+
+ This property holds the name of the locale used to interpret the number.
+
+ \sa QML:Qt::locale()
+*/
+
+QString QQuickDoubleValidator::localeName() const
+{
+ return locale().name();
+}
+
+void QQuickDoubleValidator::setLocaleName(const QString &name)
+{
+ if (locale().name() != name) {
+ setLocale(QLocale(name));
+ emit localeNameChanged();
+ }
+}
+
+void QQuickDoubleValidator::resetLocaleName()
+{
+ QLocale defaultLocale;
+ if (locale() != defaultLocale) {
+ setLocale(defaultLocale);
+ emit localeNameChanged();
+ }
+}
+
/*!
\qmlproperty real QtQuick2::DoubleValidator::top
// so you may need to run their tests first. All validators are checked
// here to ensure that their exposure to QML is working.
+ QLocale::setDefault(QLocale(QStringLiteral("C")));
+
QQuickView canvas(testFileUrl("validators.qml"));
canvas.show();
canvas.requestActivateWindow();
QVERIFY(canvas.rootObject() != 0);
+ QLocale defaultLocale;
+ QLocale enLocale("en");
+ QLocale deLocale("de_DE");
+
QQuickTextInput *intInput = qobject_cast<QQuickTextInput *>(qvariant_cast<QObject *>(canvas.rootObject()->property("intInput")));
QVERIFY(intInput);
QSignalSpy intSpy(intInput, SIGNAL(acceptableInputChanged()));
+
+ QQuickIntValidator *intValidator = qobject_cast<QQuickIntValidator *>(intInput->validator());
+ QVERIFY(intValidator);
+ QCOMPARE(intValidator->localeName(), defaultLocale.name());
+ QCOMPARE(intInput->validator()->locale(), defaultLocale);
+ intValidator->setLocaleName(enLocale.name());
+ QCOMPARE(intValidator->localeName(), enLocale.name());
+ QCOMPARE(intInput->validator()->locale(), enLocale);
+ intValidator->resetLocaleName();
+ QCOMPARE(intValidator->localeName(), defaultLocale.name());
+ QCOMPARE(intInput->validator()->locale(), defaultLocale);
+
intInput->setFocus(true);
QTRY_VERIFY(intInput->hasActiveFocus());
QCOMPARE(intInput->hasAcceptableInput(), false);
QCOMPARE(intInput->hasAcceptableInput(), false);
QCOMPARE(intInput->property("acceptable").toBool(), false);
QCOMPARE(intSpy.count(), 0);
+ QTest::keyPress(&canvas, Qt::Key_Period);
+ QTest::keyRelease(&canvas, Qt::Key_Period, Qt::NoModifier ,10);
+ QTest::qWait(50);
+ QTRY_COMPARE(intInput->text(), QLatin1String("1"));
+ QCOMPARE(intInput->hasAcceptableInput(), false);
+ QTest::keyPress(&canvas, Qt::Key_Comma);
+ QTest::keyRelease(&canvas, Qt::Key_Comma, Qt::NoModifier ,10);
+ QTest::qWait(50);
+ QTRY_COMPARE(intInput->text(), QLatin1String("1,"));
+ QCOMPARE(intInput->hasAcceptableInput(), false);
+ QTest::keyPress(&canvas, Qt::Key_Backspace);
+ QTest::keyRelease(&canvas, Qt::Key_Backspace, Qt::NoModifier ,10);
+ QTest::qWait(50);
+ QTRY_COMPARE(intInput->text(), QLatin1String("1"));
+ QCOMPARE(intInput->hasAcceptableInput(), false);
+ intValidator->setLocaleName(deLocale.name());
+ QTest::keyPress(&canvas, Qt::Key_Period);
+ QTest::keyRelease(&canvas, Qt::Key_Period, Qt::NoModifier ,10);
+ QTest::qWait(50);
+ QTRY_COMPARE(intInput->text(), QLatin1String("1."));
+ QCOMPARE(intInput->hasAcceptableInput(), false);
+ QTest::keyPress(&canvas, Qt::Key_Backspace);
+ QTest::keyRelease(&canvas, Qt::Key_Backspace, Qt::NoModifier ,10);
+ QTest::qWait(50);
+ QTRY_COMPARE(intInput->text(), QLatin1String("1"));
+ QCOMPARE(intInput->hasAcceptableInput(), false);
+ intValidator->resetLocaleName();
QTest::keyPress(&canvas, Qt::Key_1);
QTest::keyRelease(&canvas, Qt::Key_1, Qt::NoModifier ,10);
QTest::qWait(50);
QQuickTextInput *dblInput = qobject_cast<QQuickTextInput *>(qvariant_cast<QObject *>(canvas.rootObject()->property("dblInput")));
QVERIFY(dblInput);
QSignalSpy dblSpy(dblInput, SIGNAL(acceptableInputChanged()));
+
+ QQuickDoubleValidator *dblValidator = qobject_cast<QQuickDoubleValidator *>(dblInput->validator());
+ QVERIFY(dblValidator);
+ QCOMPARE(dblValidator->localeName(), defaultLocale.name());
+ QCOMPARE(dblInput->validator()->locale(), defaultLocale);
+ dblValidator->setLocaleName(enLocale.name());
+ QCOMPARE(dblValidator->localeName(), enLocale.name());
+ QCOMPARE(dblInput->validator()->locale(), enLocale);
+ dblValidator->resetLocaleName();
+ QCOMPARE(dblValidator->localeName(), defaultLocale.name());
+ QCOMPARE(dblInput->validator()->locale(), defaultLocale);
+
dblInput->setFocus(true);
QVERIFY(dblInput->hasActiveFocus() == true);
QCOMPARE(dblInput->hasAcceptableInput(), false);
QCOMPARE(dblInput->hasAcceptableInput(), true);
QCOMPARE(dblInput->property("acceptable").toBool(), true);
QCOMPARE(dblSpy.count(), 1);
+ QTest::keyPress(&canvas, Qt::Key_Comma);
+ QTest::keyRelease(&canvas, Qt::Key_Comma, Qt::NoModifier ,10);
+ QTest::qWait(50);
+ QTRY_COMPARE(dblInput->text(), QLatin1String("12,"));
+ QCOMPARE(dblInput->hasAcceptableInput(), true);
+ QTest::keyPress(&canvas, Qt::Key_1);
+ QTest::keyRelease(&canvas, Qt::Key_1, Qt::NoModifier ,10);
+ QTest::qWait(50);
+ QTRY_COMPARE(dblInput->text(), QLatin1String("12,"));
+ QCOMPARE(dblInput->hasAcceptableInput(), true);
+ dblValidator->setLocaleName(deLocale.name());
+ QCOMPARE(dblInput->hasAcceptableInput(), true);
+ QTest::keyPress(&canvas, Qt::Key_1);
+ QTest::keyRelease(&canvas, Qt::Key_1, Qt::NoModifier ,10);
+ QTest::qWait(50);
+ QTRY_COMPARE(dblInput->text(), QLatin1String("12,1"));
+ QCOMPARE(dblInput->hasAcceptableInput(), true);
+ QTest::keyPress(&canvas, Qt::Key_1);
+ QTest::keyRelease(&canvas, Qt::Key_1, Qt::NoModifier ,10);
+ QTest::qWait(50);
+ QTRY_COMPARE(dblInput->text(), QLatin1String("12,11"));
+ QCOMPARE(dblInput->hasAcceptableInput(), true);
+ QTest::keyPress(&canvas, Qt::Key_Backspace);
+ QTest::keyRelease(&canvas, Qt::Key_Backspace, Qt::NoModifier ,10);
+ QTest::qWait(50);
+ QTRY_COMPARE(dblInput->text(), QLatin1String("12,1"));
+ QCOMPARE(dblInput->hasAcceptableInput(), true);
+ QTest::keyPress(&canvas, Qt::Key_Backspace);
+ QTest::keyRelease(&canvas, Qt::Key_Backspace, Qt::NoModifier ,10);
+ QTest::qWait(50);
+ QTRY_COMPARE(dblInput->text(), QLatin1String("12,"));
+ QCOMPARE(dblInput->hasAcceptableInput(), true);
+ QTest::keyPress(&canvas, Qt::Key_Backspace);
+ QTest::keyRelease(&canvas, Qt::Key_Backspace, Qt::NoModifier ,10);
+ QTest::qWait(50);
+ QTRY_COMPARE(dblInput->text(), QLatin1String("12"));
+ QCOMPARE(dblInput->hasAcceptableInput(), true);
+ dblValidator->resetLocaleName();
QTest::keyPress(&canvas, Qt::Key_Period);
QTest::keyRelease(&canvas, Qt::Key_Period, Qt::NoModifier ,10);
QTest::qWait(50);