How to end line with QTextEdit
Asked Answered
A

3

5

I'm trying to create QTextEdit with some text, and in this text I have end of line characters (\n), but it is not accepted in QTextEdit object (whole text is displayed without any breaks). Any reason why?

Android answered 31/1, 2010 at 20:19 Comment(0)
R
8

If you're using Qt 4.3 or later, then you can use setPlainText(const QString &text)

You can turn off rich text editing with setAcceptRichText(bool accept) (Qt 4.1 or later)

Ribbonwood answered 31/1, 2010 at 20:50 Comment(0)
M
3

Per the docs, if you setTextFormat(PlainText) you should get what you want -- quoting with added emphasis:

QTextEdit works on paragraphs and characters. A paragraph is a formatted string which is word-wrapped to fit into the width of the widget. By default when reading plain text, one newline signify a paragraph. A document consists of zero or more paragraphs, indexed from 0. Characters are indexed on a per-paragraph basis, also indexed from 0. The words in the paragraph are aligned in accordance with the paragraph's alignment(). Paragraphs are separated by hard line breaks. Each character within a paragraph has its own attributes, for example, font and color.

Morganatic answered 31/1, 2010 at 20:26 Comment(3)
Funny enough, I seem not to have this function in my QTextEdit class. Any ideas why?Android
@atch, what version of Qt are you using? Older versions didn't have that exact function (though there might be others you could use, depending on what version you're stuck with -- we, of course, have no way of guessing which Qt version you may be using!).Morganatic
It could be a QT4 -> QT5 change.Unorganized
D
0

For PyQt5, new line was not showing rather entire text was shown as a string.

Firstly, set the object to accept rich text.

self.massorder.setAcceptRichText(True)

Secondly,

_data = self.massorder.toPlainText().split("\n")
Dovap answered 21/11, 2021 at 23:9 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.