PDA

View Full Version : How to get the line height of a QTextBlock?



therefore
3rd February 2013, 22:58
Windows 7 SP1
MSVS 2010
Qt 4.8.4

Given this code:


#include <QTGui>

int main(int argc, char *argv[])
{
QTextDocument* text_document = new QTextDocument("testing");
QTextBlock text_block = text_document->begin();
qDebug() << text_block.text() << text_block.blockFormat().lineHeight()
<< text_block.blockFormat().lineHeightType();
}

The console displays:

"testing" 0 0

Question: Why doesn't lineHeight return "the LineHeight property for the paragraph"? The lineHeightType is set for single spacing.

Even trying:


qDebug() << text_block.text() << text_block.layout()->boundingRect().height();

gives me zero.

alrawab
4th February 2013, 00:00
QFontMetrics fm(font);//set to any font =>font.setFamily("Arial"); font.setPointSize(10);
int yCharOffset = 4;
int lineHeight = fm.boundingRect('W').height() + 2 * yCharOffset;
int xCharOffset = 1;
int charWidth = fm.boundingRect('W').width() + 2 * xCharOffset;