Refresh the TextInput.acceptableInput property when validator changes.
authorAndrew den Exter <andrew.den-exter@nokia.com>
Thu, 21 Jun 2012 06:44:30 +0000 (16:44 +1000)
committerQt by Nokia <qt-info@nokia.com>
Wed, 4 Jul 2012 01:35:18 +0000 (03:35 +0200)
Task-number: QTBUG-26260
Change-Id: I404640d9a2f000976887dcc2119f971c17a71c7e
Reviewed-by: Martin Jones <martin.jones@nokia.com>
src/quick/items/qquicktextinput.cpp
src/quick/items/qquicktextinput_p.h
tests/auto/quick/qquicktextinput/tst_qquicktextinput.cpp

index c1e30a9..ce2492d 100644 (file)
@@ -1053,14 +1053,32 @@ void QQuickTextInput::setValidator(QValidator* v)
     if (d->m_validator == v)
         return;
 
+    if (d->m_validator) {
+        qmlobject_disconnect(
+                d->m_validator, QValidator, SIGNAL(changed()),
+                this, QQuickTextInput, SLOT(q_validatorChanged()));
+    }
+
     d->m_validator = v;
 
+    if (d->m_validator) {
+        qmlobject_connect(
+                d->m_validator, QValidator, SIGNAL(changed()),
+                this, QQuickTextInput, SLOT(q_validatorChanged()));
+    }
+
     if (isComponentComplete())
         d->checkIsValid();
 
     emit validatorChanged();
 }
 
+void QQuickTextInput::q_validatorChanged()
+{
+    Q_D(QQuickTextInput);
+    d->checkIsValid();
+}
+
 #endif // QT_NO_VALIDATOR
 
 void QQuickTextInputPrivate::checkIsValid()
index fb21eb9..16faa01 100644 (file)
@@ -340,6 +340,10 @@ private Q_SLOTS:
     void q_updateAlignment();
     void triggerPreprocess();
 
+#ifndef QT_NO_VALIDATOR
+    void q_validatorChanged();
+#endif
+
 private:
     friend class QQuickTextUtil;
 
index 73cf2d3..d2d8d5a 100644 (file)
@@ -2031,6 +2031,16 @@ void tst_qquicktextinput::validators()
     QCOMPARE(dblInput->hasAcceptableInput(), true);
     QCOMPARE(dblSpy.count(), 3);
 
+    // Changing the validator properties will re-evaluate whether the input is acceptable.
+    intValidator->setTop(10);
+    QCOMPARE(dblInput->property("acceptable").toBool(), false);
+    QCOMPARE(dblInput->hasAcceptableInput(), false);
+    QCOMPARE(dblSpy.count(), 4);
+    intValidator->setTop(12);
+    QCOMPARE(dblInput->property("acceptable").toBool(), true);
+    QCOMPARE(dblInput->hasAcceptableInput(), true);
+    QCOMPARE(dblSpy.count(), 5);
+
     QQuickTextInput *strInput = qobject_cast<QQuickTextInput *>(qvariant_cast<QObject *>(canvas.rootObject()->property("strInput")));
     QVERIFY(strInput);
     QSignalSpy strSpy(strInput, SIGNAL(acceptableInputChanged()));