How to get the character at the current cursor position in QLineEdit
-
@
QChar currentChar = lineEdit->text().at(lineEdit->cursorPosition());
@This returns the char right of the vertical line indicating the cursor.
currentChar is NOT valid, if the cursor is at the end of the line edit (i.e. no char right of the vertical line)
-
My first example was not quite correct. To cite Qt Docs
bq. API docs on "QString::at() ":http://doc.qt.nokia.com/stable/qstring.html#at
The position must be a valid index position in the string (i.e., 0 <= position < size()).So better have a check:
@
int currentPos = lineEdit->cursorPosition();
QString currentText = lineEdit->text();
QChar currentChar;
if(currentPos < currentText.size())
currentChar = currentText.at(currentPos);
@ -
hi,
Here is the same question ,
how can i add a text at the current cursor position in QLineEdit? -
@kishore_hemmady
What does http://doc.qt.io/qt-5/qlineedit.html#insert do? -
hi @JonB
I need to find the position of the cursor and append my text at that position.
your answer adds only new text,not at the defined position of a current text. -
@kishore_hemmady
It says:Deletes any selected text, inserts newText,
so doesn't it insert at the current selection position?
-
thank you @JonB