How to setText for QPlainTextEdit?

余生长醉 提交于 2019-12-10 01:19:25

问题


Qt5's documentation doesn't mention that QPlainTextEdit has setText(QString) like QTextEdit does. But, I don't think it's impossible. The only way I found is to use QTextDocument which can has setPlainText(const QString& text). So I have to do this:

plain_text_edit->setDocument(text_document);

The problem is text_document should be a pointer. Not like QTextEdit's setText which can take a local variable as it's parameter. So, is there anyway to do setText like to QPlainTextEdit?


回答1:


It's very simple, just get the current document and set its text:

plain_text_edit->document()->setPlainText(text);

Alternative way, just call this method:

plain_text_edit->setPlainText(text);

You could also use text cursor of the editor in many ways to achieve this, most simply by selecting entire existing text (assuming the editor is not empty), then doing plain_text_edit->TextCursor().insertText(text); (which replaces currently selected text with usual paste semantics), but for the simple case of replacing all text, that's overcomplicated.



来源:https://stackoverflow.com/questions/30408982/how-to-settext-for-qplaintextedit

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!