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 <marc.mutz@kdab.com>
This commit is contained in:
Daniel Teske 2017-06-27 15:12:05 +02:00 committed by Simon Hausmann
parent d39ec44e19
commit 854fdaaf76
3 changed files with 42 additions and 0 deletions

View File

@ -954,6 +954,8 @@ QString QLineEdit::selectedText() const
line edit or -1 if no text is selected. line edit or -1 if no text is selected.
\sa selectedText() \sa selectedText()
\sa selectionEnd()
\sa selectionLength()
*/ */
int QLineEdit::selectionStart() const int QLineEdit::selectionStart() const
@ -962,8 +964,33 @@ int QLineEdit::selectionStart() const
return d->control->selectionStart(); 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. Selects text from position \a start and for \a length characters.

View File

@ -155,6 +155,8 @@ public:
bool hasSelectedText() const; bool hasSelectedText() const;
QString selectedText() const; QString selectedText() const;
int selectionStart() const; int selectionStart() const;
int selectionEnd() const;
int selectionLength() const;
bool isUndoAvailable() const; bool isUndoAvailable() const;
bool isRedoAvailable() const; bool isRedoAvailable() const;

View File

@ -3953,6 +3953,8 @@ void tst_QLineEdit::QTBUG16850_setSelection()
le.setText(" 1"); le.setText(" 1");
le.setSelection(3, 1); le.setSelection(3, 1);
QCOMPARE(le.selectionStart(), 3); QCOMPARE(le.selectionStart(), 3);
QCOMPARE(le.selectionEnd(), 4);
QCOMPARE(le.selectionLength(), 1);
QCOMPARE(le.selectedText(), QString("1")); QCOMPARE(le.selectedText(), QString("1"));
} }
@ -4147,11 +4149,16 @@ void tst_QLineEdit::inputMethodSelection()
QCOMPARE(selectionSpy.count(), 0); QCOMPARE(selectionSpy.count(), 0);
QCOMPARE(testWidget->selectionStart(), -1); QCOMPARE(testWidget->selectionStart(), -1);
QCOMPARE(testWidget->selectionEnd(), -1);
QCOMPARE(testWidget->selectionLength(), 0);
testWidget->setSelection(0,5); testWidget->setSelection(0,5);
QCOMPARE(selectionSpy.count(), 1); QCOMPARE(selectionSpy.count(), 1);
QCOMPARE(testWidget->selectionStart(), 0); QCOMPARE(testWidget->selectionStart(), 0);
QCOMPARE(testWidget->selectionEnd(), 5);
QCOMPARE(testWidget->selectionLength(), 5);
// selection gained // selection gained
{ {
@ -4163,6 +4170,8 @@ void tst_QLineEdit::inputMethodSelection()
QCOMPARE(selectionSpy.count(), 2); QCOMPARE(selectionSpy.count(), 2);
QCOMPARE(testWidget->selectionStart(), 12); QCOMPARE(testWidget->selectionStart(), 12);
QCOMPARE(testWidget->selectionEnd(), 17);
QCOMPARE(testWidget->selectionLength(), 5);
// selection removed // selection removed
{ {
@ -4173,6 +4182,10 @@ void tst_QLineEdit::inputMethodSelection()
} }
QCOMPARE(selectionSpy.count(), 3); QCOMPARE(selectionSpy.count(), 3);
QCOMPARE(testWidget->selectionStart(), -1);
QCOMPARE(testWidget->selectionEnd(), -1);
QCOMPARE(testWidget->selectionLength(), 0);
} }
Q_DECLARE_METATYPE(Qt::InputMethodHints) Q_DECLARE_METATYPE(Qt::InputMethodHints)