View Full Version : QTextEdit and QPlainTextEdit Wrapping

26th June 2014, 08:40

someone knows how these two widgets do paragraph warpping? e.g 25 characters one line.

The goal is to specify a certain block's line length, not the whole document.


im paragraph 1 im paragraph 1 im paragraph 1 im paragraph 1 im paragraph 1

im paragraph 2
im paragraph 2
im paragraph 2
im paragraph 2

it could be achieved by adding paragraphbreak at the end of divided lines (so simple)
but i wonder whether i could do it without the hack, because adding break turns the paragraph 2 into 3 4 5...

issue update:

a seemingly working solution after some digging:

// textLayout inherits QPlainTextDocumentLayout

QRectF textLayout :: blockBoundingRect(const QTextBlock& block) const
// check whether the block is target block need to be wrapped
// ...
// line width in pixel

if (...) {
QTextLayout* layout = block.layout();

qreal height = 0;

while (true) {
QTextLine line = layout->createLine();
if (! line.isValid())

line.setPosition(QPointF(0, height));
height += line.height();

return QPlainTextDocumentLayout::blockBoundingRect(block) ;

The text is fine wrapped at the paragraph, but ... scrollbar never comes, what's more, it only works under WidgetWrap Mode.

so the new issues extended:

how to make the scrollbar come?