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();
if(currentPos < currentText.size())
currentChar = currentText.at(currentPos);
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()