From cc37d494c2c6ef5768e4bc30cad62f24fee033c3 Mon Sep 17 00:00:00 2001 From: Olivier Goffart Date: Thu, 22 Apr 2010 12:10:50 +0200 Subject: Fix regression: auto completion text cursor problem in Q3FileDialog This was another regression in QLineEdit introduced with the move to QLineControl setCursorPosition clear the selection, and setSelection move the cursor position. So the case where we want the position at the beginning of the selection need to be handled separately (by doing the selection reverse) This does not cover the case where the position is in the middle, or outside the selection, but it is not possible to do. Task-number: QTBUG-10019 Reviewed-by: jbache --- src/gui/widgets/qlineedit.cpp | 10 ++++++++-- tests/auto/qlineedit/qlineedit.pro | 1 + tests/auto/qlineedit/tst_qlineedit.cpp | 33 +++++++++++++++++++++++++++++++++ 3 files changed, 42 insertions(+), 2 deletions(-) diff --git a/src/gui/widgets/qlineedit.cpp b/src/gui/widgets/qlineedit.cpp index 2d2df92..f041a36 100644 --- a/src/gui/widgets/qlineedit.cpp +++ b/src/gui/widgets/qlineedit.cpp @@ -738,8 +738,14 @@ bool QLineEdit::validateAndSet(const QString &newText, int newPos, setText(oldText); return false; } - setCursorPosition(newPos); - setSelection(qMin(newMarkAnchor, newMarkDrag), qAbs(newMarkAnchor - newMarkDrag)); + int selstart = qMin(newMarkAnchor, newMarkDrag); + int sellength = qAbs(newMarkAnchor - newMarkDrag); + if (selstart == newPos) { + selstart = qMax(newMarkAnchor, newMarkDrag); + sellength = -sellength; + } + //setSelection also set the position + setSelection(selstart, sellength); return true; } #endif //QT3_SUPPORT diff --git a/tests/auto/qlineedit/qlineedit.pro b/tests/auto/qlineedit/qlineedit.pro index f00a2d8..88527ce 100644 --- a/tests/auto/qlineedit/qlineedit.pro +++ b/tests/auto/qlineedit/qlineedit.pro @@ -1,4 +1,5 @@ load(qttest_p4) +QT += qt3support SOURCES += tst_qlineedit.cpp diff --git a/tests/auto/qlineedit/tst_qlineedit.cpp b/tests/auto/qlineedit/tst_qlineedit.cpp index ca84b38..592c1cb 100644 --- a/tests/auto/qlineedit/tst_qlineedit.cpp +++ b/tests/auto/qlineedit/tst_qlineedit.cpp @@ -273,6 +273,11 @@ private slots: void taskQTBUG_4679_selectToStartEndOfBlock(); void taskQTBUG_7395_readOnlyShortcut(); +#ifdef QT3_SUPPORT + void validateAndSet_data(); + void validateAndSet(); +#endif + protected slots: #ifdef QT3_SUPPORT void lostFocus(); @@ -1488,6 +1493,34 @@ void tst_QLineEdit::lostFocus() { editingFinished(); } + +void tst_QLineEdit::validateAndSet_data() +{ + QTest::addColumn("newText"); + QTest::addColumn("newPos"); + QTest::addColumn("newMarkAnchor"); + QTest::addColumn("newMarkDrag"); + + QTest::newRow("1") << QString("Hello World") << 3 << 3 << 5; + QTest::newRow("2") << QString("Hello World") << 5 << 3 << 5; +} + +void tst_QLineEdit::validateAndSet() +{ + QFETCH(QString, newText); + QFETCH(int, newPos); + QFETCH(int, newMarkAnchor); + QFETCH(int, newMarkDrag); + + QLineEdit e; + e.validateAndSet(newText, newPos, newMarkAnchor, newMarkDrag); + QCOMPARE(e.text(), newText); + QCOMPARE(e.cursorPosition(), newPos); + QCOMPARE(e.selectedText(), newText.mid(newMarkAnchor, newMarkDrag-newMarkAnchor)); +} + + + #endif void tst_QLineEdit::editingFinished() { -- cgit v0.12 From e34b0cc2766ce403a8d12964039957f45e35e451 Mon Sep 17 00:00:00 2001 From: Olivier Goffart Date: Fri, 23 Apr 2010 09:24:56 +0200 Subject: tst_qlineedit: add contains(QT_CONFIG,qt3support) to the .pro file to make it compile --- tests/auto/qlineedit/qlineedit.pro | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/auto/qlineedit/qlineedit.pro b/tests/auto/qlineedit/qlineedit.pro index 88527ce..1f862b4 100644 --- a/tests/auto/qlineedit/qlineedit.pro +++ b/tests/auto/qlineedit/qlineedit.pro @@ -1,5 +1,5 @@ load(qttest_p4) -QT += qt3support +contains(QT_CONFIG,qt3support) QT += qt3support SOURCES += tst_qlineedit.cpp -- cgit v0.12