本文介绍了使用QTextCursor选择一段文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
使用Qt框架选择文本时遇到问题。例如,如果我有这个文件:没有时间休息。我想选择ime for r并从文档中删除这一段文字,我应该如何使用QTextCursor?这是我的代码:
Having problems with selecting pieces of text using the Qt framework. For example if i have this document : "No time for rest". And i want to select "ime for r" and delete this piece of text from the document, how should i do it using QTextCursor? Here is my code:
c。使用 removeSelectedText()。
- cursor->select(QTextCursor::LineUnderCursor); line selects whole current line. You don't want to delete whole line, so why would you write this? Remove this line of code.
- clearSelection() just deselects everything. Use removeSelectedText() instead.
- Don't create QTextCursor using new. It's correct but not needed. You should avoid pointers when possible. QTextCursor is usually passed by value or reference. Also you can use QPlainText:textCursor to get a copy of the edit cursor.
所以,代码应该像这样:
So, the code should look like that:
QTextCursor cursor = ui->plainTextEdit->textCursor(); cursor.setPosition(StartPos, QTextCursor::MoveAnchor); cursor.setPosition(EndPos, QTextCursor::KeepAnchor); cursor.removeSelectedText();
这篇关于使用QTextCursor选择一段文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!