From 854fdaaf76873a4f5c6d5a5046b35bc8152fcc24 Mon Sep 17 00:00:00 2001 From: Daniel Teske Date: Tue, 27 Jun 2017 15:12:05 +0200 Subject: [PATCH] QLineEdit: Add selectionEnd() and selectionLength() Getting the end position of the selection was not possible. [ChangeLog][QtWidgets][QLineEdit] Added selectionEnd(), selectionLength(), complementing selectionStart(). Change-Id: Iaecc624063d7c043f9502351f07eb76f869e86f1 Reviewed-by: Marc Mutz --- src/widgets/widgets/qlineedit.cpp | 27 +++++++++++++++++++ src/widgets/widgets/qlineedit.h | 2 ++ .../widgets/qlineedit/tst_qlineedit.cpp | 13 +++++++++ 3 files changed, 42 insertions(+) diff --git a/src/widgets/widgets/qlineedit.cpp b/src/widgets/widgets/qlineedit.cpp index 4ac8118c87..f859ec0ddb 100644 --- a/src/widgets/widgets/qlineedit.cpp +++ b/src/widgets/widgets/qlineedit.cpp @@ -954,6 +954,8 @@ QString QLineEdit::selectedText() const line edit or -1 if no text is selected. \sa selectedText() + \sa selectionEnd() + \sa selectionLength() */ int QLineEdit::selectionStart() const @@ -962,8 +964,33 @@ int QLineEdit::selectionStart() const return d->control->selectionStart(); } +/*! + Returns the index of the character directly after the selection + in the line edit or -1 if no text is selected. + \since 5.10 + \sa selectedText() + \sa selectionStart() + \sa selectionLength() +*/ +int QLineEdit::selectionEnd() const +{ + Q_D(const QLineEdit); + return d->control->selectionEnd(); +} +/*! + Returns the length of the selection. + \since 5.10 + + \sa selectedText() + \sa selectionStart() + \sa selectionEnd() +*/ +int QLineEdit::selectionLength() const +{ + return selectionEnd() - selectionStart(); +} /*! Selects text from position \a start and for \a length characters. diff --git a/src/widgets/widgets/qlineedit.h b/src/widgets/widgets/qlineedit.h index 4d32b11f06..d84eeedaf6 100644 --- a/src/widgets/widgets/qlineedit.h +++ b/src/widgets/widgets/qlineedit.h @@ -155,6 +155,8 @@ public: bool hasSelectedText() const; QString selectedText() const; int selectionStart() const; + int selectionEnd() const; + int selectionLength() const; bool isUndoAvailable() const; bool isRedoAvailable() const; diff --git a/tests/auto/widgets/widgets/qlineedit/tst_qlineedit.cpp b/tests/auto/widgets/widgets/qlineedit/tst_qlineedit.cpp index 3f7fdd6f6f..fa4a2f1da3 100644 --- a/tests/auto/widgets/widgets/qlineedit/tst_qlineedit.cpp +++ b/tests/auto/widgets/widgets/qlineedit/tst_qlineedit.cpp @@ -3953,6 +3953,8 @@ void tst_QLineEdit::QTBUG16850_setSelection() le.setText(" 1"); le.setSelection(3, 1); QCOMPARE(le.selectionStart(), 3); + QCOMPARE(le.selectionEnd(), 4); + QCOMPARE(le.selectionLength(), 1); QCOMPARE(le.selectedText(), QString("1")); } @@ -4147,11 +4149,16 @@ void tst_QLineEdit::inputMethodSelection() QCOMPARE(selectionSpy.count(), 0); QCOMPARE(testWidget->selectionStart(), -1); + QCOMPARE(testWidget->selectionEnd(), -1); + QCOMPARE(testWidget->selectionLength(), 0); testWidget->setSelection(0,5); QCOMPARE(selectionSpy.count(), 1); QCOMPARE(testWidget->selectionStart(), 0); + QCOMPARE(testWidget->selectionEnd(), 5); + QCOMPARE(testWidget->selectionLength(), 5); + // selection gained { @@ -4163,6 +4170,8 @@ void tst_QLineEdit::inputMethodSelection() QCOMPARE(selectionSpy.count(), 2); QCOMPARE(testWidget->selectionStart(), 12); + QCOMPARE(testWidget->selectionEnd(), 17); + QCOMPARE(testWidget->selectionLength(), 5); // selection removed { @@ -4173,6 +4182,10 @@ void tst_QLineEdit::inputMethodSelection() } QCOMPARE(selectionSpy.count(), 3); + QCOMPARE(testWidget->selectionStart(), -1); + QCOMPARE(testWidget->selectionEnd(), -1); + QCOMPARE(testWidget->selectionLength(), 0); + } Q_DECLARE_METATYPE(Qt::InputMethodHints)