There are several issues in your code:
cursor->select(QTextCursor::LineUnderCursor);
line selects whole current line. You don't want to delete whole line, so why would you write this? Remove this line of code.clearSelection()
just deselects everything. UseremoveSelectedText()
instead.- Don't create
QTextCursor
usingnew
. It's correct but not needed. You should avoid pointers when possible.QTextCursor
is usually passed by value or reference. Also you can useQPlainTextEdit::textCursor
to get a copy of the edit cursor.
So, the code should look like that:
QTextCursor cursor = ui->plainTextEdit->textCursor();
cursor.setPosition(StartPos, QTextCursor::MoveAnchor);
cursor.setPosition(EndPos, QTextCursor::KeepAnchor);
cursor.removeSelectedText();