From 802921d40b8b48239958c6035c74d986fe606860 Mon Sep 17 00:00:00 2001 From: Liang Qi Date: Thu, 21 Nov 2013 11:06:24 +0100 Subject: [PATCH] TextInput: add editingFinished signal Autotest is included. Task-number: QTBUG-34780 [ChangeLog][QtDeclarative][TextInput] add editingFinished signal Change-Id: Ib633daee67cd4e5f15739a6004adbe882ab3d3fc Reviewed-by: Lars Knoll Reviewed-by: J-P Nurmi Reviewed-by: Thomas Hartmann Reviewed-by: Jens Bache-Wiig Reviewed-by: Alan Alpert (Personal) <416365416c@gmail.com> --- src/quick/items/qquickitemsmodule.cpp | 1 + src/quick/items/qquicktextinput.cpp | 15 ++++++++ src/quick/items/qquicktextinput_p.h | 1 + .../data/signal_editingfinished.qml | 13 +++++++ .../quick/qquicktextinput/tst_qquicktextinput.cpp | 45 ++++++++++++++++++++++ 5 files changed, 75 insertions(+) create mode 100644 tests/auto/quick/qquicktextinput/data/signal_editingfinished.qml diff --git a/src/quick/items/qquickitemsmodule.cpp b/src/quick/items/qquickitemsmodule.cpp index ebc32c8..a5b78b2 100644 --- a/src/quick/items/qquickitemsmodule.cpp +++ b/src/quick/items/qquickitemsmodule.cpp @@ -188,6 +188,7 @@ static void qt_quickitems_defineModule(const char *uri, int major, int minor) qmlRegisterType(uri,major,minor,"TextEdit"); qmlRegisterType(uri,2,1,"TextEdit"); qmlRegisterType(uri,major,minor,"TextInput"); + qmlRegisterType(uri,2,2,"TextInput"); qmlRegisterType(uri,major,minor,"ViewSection"); qmlRegisterType(); diff --git a/src/quick/items/qquicktextinput.cpp b/src/quick/items/qquicktextinput.cpp index 93ea677..b46387b 100644 --- a/src/quick/items/qquicktextinput.cpp +++ b/src/quick/items/qquicktextinput.cpp @@ -1212,6 +1212,17 @@ bool QQuickTextInput::hasAcceptableInput() const state. */ +/*! + \qmlsignal QtQuick::TextInput::onEditingFinished() + \since 5.2 + + This handler is called when the Return or Enter key is pressed or + the text input loses focus. Note that if there is a validator or + inputMask set on the text input and enter/return is pressed, this + handler will only be called if the input follows + the inputMask and the validator returns an acceptable state. +*/ + #ifndef QT_NO_IM Qt::InputMethodHints QQuickTextInputPrivate::effectiveInputMethodHints() const { @@ -2522,6 +2533,9 @@ void QQuickTextInputPrivate::handleFocusEvent(QFocusEvent *event) && !persistentSelection) deselect(); + if (hasAcceptableInput(m_text) || fixup()) + emit q->editingFinished(); + #ifndef QT_NO_IM q->disconnect(qApp->inputMethod(), SIGNAL(inputDirectionChanged(Qt::LayoutDirection)), q, SLOT(q_updateAlignment())); @@ -4105,6 +4119,7 @@ void QQuickTextInputPrivate::processKeyEvent(QKeyEvent* event) if (event->key() == Qt::Key_Enter || event->key() == Qt::Key_Return) { if (hasAcceptableInput(m_text) || fixup()) { emit q->accepted(); + emit q->editingFinished(); } event->ignore(); return; diff --git a/src/quick/items/qquicktextinput_p.h b/src/quick/items/qquicktextinput_p.h index 2b72afb..5f0250a 100644 --- a/src/quick/items/qquicktextinput_p.h +++ b/src/quick/items/qquicktextinput_p.h @@ -284,6 +284,7 @@ Q_SIGNALS: void selectedTextChanged(); void accepted(); void acceptableInputChanged(); + Q_REVISION(2) void editingFinished(); void colorChanged(); void selectionColorChanged(); void selectedTextColorChanged(); diff --git a/tests/auto/quick/qquicktextinput/data/signal_editingfinished.qml b/tests/auto/quick/qquicktextinput/data/signal_editingfinished.qml new file mode 100644 index 0000000..2ec5ce6 --- /dev/null +++ b/tests/auto/quick/qquicktextinput/data/signal_editingfinished.qml @@ -0,0 +1,13 @@ +import QtQuick 2.2 + +Item { + property variant input1: input1 + property variant input2: input2 + + width: 800; height: 600; + + Column{ + TextInput { id: input1; } + TextInput { id: input2; } + } +} diff --git a/tests/auto/quick/qquicktextinput/tst_qquicktextinput.cpp b/tests/auto/quick/qquicktextinput/tst_qquicktextinput.cpp index 1c2ab4d..8072672 100644 --- a/tests/auto/quick/qquicktextinput/tst_qquicktextinput.cpp +++ b/tests/auto/quick/qquicktextinput/tst_qquicktextinput.cpp @@ -146,6 +146,8 @@ private slots: void validators(); void inputMethods(); + void signal_editingfinished(); + void passwordCharacter(); void cursorDelegate_data(); void cursorDelegate(); @@ -2303,6 +2305,49 @@ void tst_qquicktextinput::inputMethods() QCOMPARE(enabledQueryEvent.value(Qt::ImEnabled).toBool(), false); } +void tst_qquicktextinput::signal_editingfinished() +{ + QQuickView window(testFileUrl("signal_editingfinished.qml")); + window.show(); + window.requestActivate(); + QTest::qWaitForWindowActive(&window); + + QVERIFY(window.rootObject() != 0); + + QQuickTextInput *input1 = qobject_cast(qvariant_cast(window.rootObject()->property("input1"))); + QVERIFY(input1); + QQuickTextInput *input2 = qobject_cast(qvariant_cast(window.rootObject()->property("input2"))); + QVERIFY(input2); + QSignalSpy input1Spy(input1, SIGNAL(editingFinished())); + + input1->setFocus(true); + QTRY_VERIFY(input1->hasActiveFocus()); + QTRY_VERIFY(!input2->hasActiveFocus()); + + QTest::keyPress(&window, Qt::Key_A); + QTest::keyRelease(&window, Qt::Key_A, Qt::NoModifier); + QTRY_COMPARE(input1->text(), QLatin1String("a")); + + QTest::keyPress(&window, Qt::Key_Enter); + QTest::keyRelease(&window, Qt::Key_Enter, Qt::NoModifier); + QTRY_COMPARE(input1Spy.count(), 1); + + QSignalSpy input2Spy(input2, SIGNAL(editingFinished())); + + input2->setFocus(true); + QTRY_VERIFY(!input1->hasActiveFocus()); + QTRY_VERIFY(input2->hasActiveFocus()); + + QTest::keyPress(&window, Qt::Key_A); + QTest::keyRelease(&window, Qt::Key_A, Qt::NoModifier); + QTRY_COMPARE(input2->text(), QLatin1String("a")); + + input1->setFocus(true); + QTRY_VERIFY(input1->hasActiveFocus()); + QTRY_VERIFY(!input2->hasActiveFocus()); + QTRY_COMPARE(input2Spy.count(), 1); +} + /* TextInput element should only handle left/right keys until the cursor reaches the extent of the text, then they should ignore the keys. -- 2.7.4