QTextEdit deletes the entire row at the given position

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

I also found a solution here Remove the line / block from QTextEdit , but I do not know what these blocks are. Do they represent separate lines or not? Should I iterate over these blocks and if I get the block at the given index, then delete it?

+8
c ++ user-interface text qt qt5
source share
2 answers

You can delete a line in lineNumer with

 QTextCursor cursor = textEdit->textCursor(); cursor.movePosition(QTextCursor::Start); cursor.movePosition(QTextCursor::Down, QTextCursor::MoveAnchor, lineNumer); cursor.select(QTextCursor::LineUnderCursor); cursor.removeSelectedText(); textEdit->setTextCursor(cursor); 

Here you place the cursor at the beginning of the document, omit lineNumer times, select a specific line and delete it.

+6
source share

You can do the following:

 QTextEdit te; // Three lines in the text edit te.setText("Line 1\nLine 2\nLine 3"); const int lineToDelete = 1; // To delete the second line. QTextBlock b = te.document()->findBlockByLineNumber(lineToDelete); if (b.isValid()) { QTextCursor cursor(b); cursor.select(QTextCursor::BlockUnderCursor); cursor.removeSelectedText(); } 
+1
source share

All Articles