PDA

View Full Version : QTextEdit and QPlainTextEdit Wrapping



cic1988
26th June 2014, 08:40
Hello,

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.

e.g




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();
layout->clearLayout();

qreal height = 0;
layout->beginLayout();

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

line.setLineWidth(lineWidth);
line.setPosition(QPointF(0, height));
height += line.height();
}
layout->endLayout();
}

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?