Skip to content

Instantly share code, notes, and snippets.

@jemyzhang
Last active December 26, 2015 09:29
Show Gist options
  • Save jemyzhang/7130092 to your computer and use it in GitHub Desktop.
Save jemyzhang/7130092 to your computer and use it in GitHub Desktop.
remove top old blocks from QTextEdit
QTextBlock block = document()->begin();
while(block.isValid()) {
if(document()->blockCount() > m_maxlines)
{
QTextCursor cursor(block);
block = block.next();
cursor.select(QTextCursor::BlockUnderCursor);
//move forward one character to select the return-char,
//in order to avoid the empty line after the selected text is removed.
cursor.movePosition(QTextCursor::NextCharacter,QTextCursor::KeepAnchor);
cursor.removeSelectedText();
}else{
break;
}
}
@jemyzhang
Copy link
Author

cursor.removeSelectedText();

似乎会将text删除,但是对于空行而言,selected text是“”,remove不了,如何解决这个问题?

@jemyzhang
Copy link
Author

将选择光标移动到下一个字符QTextCursor::NextCharacter,即'\n'后,将换行符一并删除,可以保证blockCount减小,从而删除正确的行数,还能避免remove后留下空行。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment