[PyQt] QTextEdit: getting text with wordwrap newlines

David Cortesi davecortesi at gmail.com
Sat Apr 26 16:47:36 BST 2014


>
> I have a QTextEdit which has word wrap enabled.  I would like to get the
> text data but have the newlines from the word wrap as well.  I basically
> need the text exactly as it appears to the user, rather than the one long
> string from toPlainText().  Is there a way to do this?
>

One, there's a difference between PyQt4 and PyQt5 and between Python 2.x
and 3.x. In Qt4, toPlainText() returns a QString, and you should find
within it a Unicode paragraph separator, \u2021 I think, wherever there is
a physical line break.

In Qt5, Phil et.al. did away with QString entirely and just returns a
Python string, which in Python 2.x will be a unicode string and in Python
3.x, just a str because str is automatically unicode. But either way, I
believe the string value ought to have \n wherever there is a line break in
the editor. Does it not?

Two, look at the QTextBlock interface. Each physical (possibly wrapped)
line in the editor is a QTextBlock. You can ask the QTextDocument (NOT the
QTextEdit) for its .begin(), use QTextBlock.next() and iterate through the
document that way. Or you can ask the document to .findBlockXXXX and get a
starting block in various ways, and use .next() or .prior() to iterate from
that line.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://www.riverbankcomputing.com/pipermail/pyqt/attachments/20140426/49c66372/attachment.html>


More information about the PyQt mailing list