line spaceing in QTextDocument
Following my previous post, i`d need custom line spacing in a QTextDocument.
No proper solution was found here:
http://www.qtcentre.org/threads/1929...t-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
Quote:
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...entlayout.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:
Re: line spaceing in QTextDocument
Hi,
you can iterate through the blocks of QTextDocument and set the line spacing with QTextBlock::blockFormat() + QTextBlockFormat::setLineHeight().
Re: line spaceing in QTextDocument
thanks!
It wont work though...
I decided to go bruteforce onthis:
Code:
testform
::testform(QWidget *parent, Qt
::WFlags flags
){
ui.setupUi(this);
ui.textEdit->setText("asdfasdfasdf\r\nbbbbbbbbbbbbbbbbbbbb\r\nccccccccccccccccc\r\nddddddddddddddddddf");
for(int i=0; i<ui.textEdit->document()->blockCount(); i++)
{
ui.
textEdit->document
()->findBlockByNumber
(i
).
blockFormat().
setLineHeight(12,
QTextBlockFormat::LineDistanceHeight);
}
}
But it doesnt work. :(
Re: line spaceing in QTextDocument
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!
Code:
te.setText("asdfasdfasdf\nbbbbbbbbbbbbbbbbbbbb\nccccccccccccccccc\nddddddddddddddddddf");
do {
c.setBlockFormat(f);
te.show();
Re: line spaceing in QTextDocument
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:
Code:
painter->translate(x, y);
document()->setTextWidth(w);
document()->setDefaultFont(painter->font());
do {
c.setBlockFormat(f);
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:
Code:
document().setPlainText("aaaaa\nbbbbbbb\ncccccc");
any idea?:)
Re: line spaceing in QTextDocument
Quote:
Originally Posted by
tuli
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.
Re: line spaceing in QTextDocument
h provide sufficient space, and
Quote:
document()->drawContents(painter);
provides same result... :/
Re: line spaceing in QTextDocument
Then please make a minimal compile able example which illustrate your problem.