Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to setText for QPlainTextEdit?

Tags:

qt

qt5

settext

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?

like image 741
Mas Bagol Avatar asked May 23 '15 04:05

Mas Bagol


People also ask

How do I get text from QTextEdit?

QTextEdit does not have any text() method, if you want to get the text you must use toPlainText() , if you want to clean the text it is better to use clear() since it makes it more readable.

How to add text in QPlainTextEdit?

Using QPlainTextEdit as a Display Widget The text is set or replaced using setPlainText() which deletes the existing text and replaces it with the text passed to setPlainText(). Text can be inserted using the QTextCursor class or using the convenience functions insertPlainText(), appendPlainText() or paste().

How do I insert a textbox in Qt?

Using Rich Text You can use rich text in the Text and Text Input components. To open the rich text editor, select the (Edit) button in Properties > Character > Text.

How do you use text browser QT?

If you want to provide your users with an editable rich text editor, use QTextEdit. If you want a text browser without hypertext navigation use QTextEdit, and use QTextEdit::setReadOnly() to disable editing. If you just need to display a small piece of rich text use QLabel.


1 Answers

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.

like image 174
hyde Avatar answered Oct 21 '22 04:10

hyde