Unsolved How to get the character at the current cursor position in QLineEdit
-
Any idea how to get the character at the current cursor position in QLineEdit
I was trying to do use Qtextcursor,But in vain. -
@
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);
@ -
Thanks,
I was doing a delete operation,so on deletion of characters,the character to the left of cursor position (vertical line )was needed,any ideas -
Come on - thats a "-1" to the current position and you have to check for "pos - 1 >= 0".
That's a no-brainer - do you really need another sample code for this?
-
Or just select the part you want to delete and call selectedText()
-
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