Last active
December 26, 2015 09:29
-
-
Save jemyzhang/7130092 to your computer and use it in GitHub Desktop.
remove top old blocks from QTextEdit
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | |
} | |
} |
将选择光标移动到下一个字符QTextCursor::NextCharacter
,即'\n'后,将换行符一并删除,可以保证blockCount减小,从而删除正确的行数,还能避免remove后留下空行。
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
似乎会将text删除,但是对于空行而言,selected text是“”,remove不了,如何解决这个问题?