Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

QTextEdit delete whole line at given position

I need to delete a specific line from QTextEdit (NoWrap option is active) manualy from the program. I found a solution which explains how to remove first line, but i wonder how can I remove whole line at specific index.

I've also found a solution here Remove a line/block from QTextEdit , but I don't know what these blocks are. Do they represent single lines or not? Should i iterate through these blocks and if i reach block at given index, then delete it?

like image 859
Michal Avatar asked Jan 07 '15 08:01

Michal


1 Answers

You can remove the line at lineNumer with :

QTextCursor cursor = textEdit->textCursor();

cursor.movePosition(QTextCursor::Start);
cursor.movePosition(QTextCursor::Down, QTextCursor::MoveAnchor, lineNumer);
cursor.select(QTextCursor::LineUnderCursor);
cursor.removeSelectedText();
cursor.deleteChar(); // clean up new line

textEdit->setTextCursor(cursor);

Here you put the cursor at the beginning of the document, move down lineNumer times, select the specific line and remove it.

like image 90
Nejat Avatar answered Sep 19 '22 12:09

Nejat