我需要从程序中手动删除 QTextEdit 中的特定行(NoWrap 选项处于事件状态)。我找到了一个解释如何删除第一行的解决方案,但我想知道如何删除特定索引处的整行。
我在这里也找到了解决方案 Remove a line/block from QTextEdit ,但我不知道这些 block 是什么。它们是否代表单行?我是否应该遍历这些 block ,如果我到达给定索引处的 block ,则删除它?
最佳答案
您可以删除 lineNumer
处的行:
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);
此处将光标放在文档的开头,向下移动lineNumer
次,选择特定行并将其删除。
关于c++ - QTextEdit删除给定位置的整行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27814909/