PDA

View Full Version : line spaceing in QTextDocument



tuli
14th July 2012, 17:34
Following my previous post, i`d need custom line spacing in a QTextDocument.

No proper solution was found here:
http://www.qtcentre.org/threads/19297-QTextDocument-line-spacing

But after the post, an addition was supposedly made to Qt in 4.8.0:
https://bugreports.qt-project.org/browse/QTBUG-14470


Status: Closed Closed
Priority: P2: Important P2: Important
Resolution: Done
Affects Version/s: 4.7.0
Fix Version/s: 4.8.0

However, the thread fails to mention what exactly was added/changed, and i am unable to find anything in Qt 4.8.1. :/

I figured i am supposed to layout every single paragraph in the document by subclassing this:
http://doc.qt.nokia.com/4.7-snapshot/qabstracttextdocumentlayout.html

but i still wouldnt know how to change the line space...


Funny, Qt offers so much awesomeness, and then it`s these little things that you are left spending time on...:eek:

Lykurg
14th July 2012, 17:54
Hi,

you can iterate through the blocks of QTextDocument and set the line spacing with QTextBlock::blockFormat() + QTextBlockFormat::setLineHeight().

tuli
15th July 2012, 05:53
thanks!

It wont work though...
I decided to go bruteforce onthis:



testform::testform(QWidget *parent, Qt::WFlags flags)
: QMainWindow(parent, flags)
{
ui.setupUi(this);


ui.textEdit->setText("asdfasdfasdf\r\nbbbbbbbbbbbbbbbbbbbb\r\ncccccccccc ccccccc\r\nddddddddddddddddddf");

for(int i=0; i<ui.textEdit->document()->blockCount(); i++)
{
ui.textEdit->document()->findBlockByNumber(i).blockFormat().setLineHeight(1 2, QTextBlockFormat::LineDistanceHeight);
}
}

But it doesnt work. :(

Lykurg
15th July 2012, 08:53
Of course it does not work. You have to set the modified format back to the document! blockFormat() simply returns a copy of the format, no pointer nor reference!
QTextEdit te;
te.setText("asdfasdfasdf\nbbbbbbbbbbbbbbbbbbbb\ncccccccccccccc ccc\nddddddddddddddddddf");
QTextCursor c(te.document());
c.movePosition(QTextCursor::Start);
do {
QTextBlockFormat f = c.blockFormat();
f.setLineHeight(12, QTextBlockFormat::LineDistanceHeight);
c.setBlockFormat(f);
} while (c.movePosition(QTextCursor::NextBlock));
te.show();

tuli
18th July 2012, 06:53
ouch...yeah, that was it. That makes a great second post, doesnt it...:(

So now i have this code to render a document to the screen:


painter->translate(x, y);
document()->setTextWidth(w);
document()->setDefaultFont(painter->font());


QTextCursor c(document());
c.movePosition(QTextCursor::Start);
do {
QTextBlockFormat f = c.blockFormat();
f.setLineHeight(h, QTextBlockFormat::LineDistanceHeight);
c.setBlockFormat(f);
} while (c.movePosition(QTextCursor::NextBlock));


document()->drawContents(painter, QRect(0,0,w,h));

This works perfectly for single-line strings, but not when strings with linebreaks in it have been assigned:


document().setPlainText("aaaaa\nbbbbbbb\ncccccc");

any idea?:)

Lykurg
18th July 2012, 07:20
any idea?:)Yes. The second parameter of drawContents clips the painting. So you have to alter "h" when you have more lines than one or just leave it empty.

tuli
18th July 2012, 13:18
h provide sufficient space, and


document()->drawContents(painter);

provides same result... :/

Lykurg
18th July 2012, 17:24
Then please make a minimal compile able example which illustrate your problem.