QTextEdit在给定位置删除整行 [英] QTextEdit delete whole line at given position
问题描述
我需要从程序中手动删除QTextEdit中的特定行(NoWrap选项处于活动状态).我找到了一个解决方案,该解决方案说明了如何删除第一行,但是我想知道如何删除特定索引处的整个行.
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.
我还在从QTextEdit删除行/块中找到了解决方案,但我不知道这些块是什么.它们代表单行吗?我应该遍历这些块吗?如果我到达给定索引的块,则将其删除?
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?
推荐答案
您可以使用以下命令删除 lineNumer
处的行:
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);
在此处将光标置于文档的开头,向下移动 lineNumer
次,选择特定的行并将其删除.
Here you put the cursor at the beginning of the document, move down lineNumer
times, select the specific line and remove it.
这篇关于QTextEdit在给定位置删除整行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!