如何确定Qt文档行的渲染高度

时间:2013-02-09 22:00:46

标签: c++ qt

Windows 7 SP1
MSVS 2010
Qt 4.8.4

我正在尝试确定Qt文档的每一行(确切的文本块)的渲染高度。该文档具有丰富的文本,因此每个文本块可能具有片段。假设没有自动换行,所以每个文本块都是一行。为简化起见,这就是我想要做的事情:

 #include <QTGui>

int CalculateLineHeight(QTextBlock text_block);

int main(int argc, char *argv[])
{
    QApplication app(argc, argv);
    QMainWindow* window = new QMainWindow;
    QTextEdit* editor = new QTextEdit(window);

    QTextDocument* text_document = new QTextDocument(window);
    editor->setDocument(text_document);

    QFile file("test.html");
    if (file.open(QFile::ReadOnly | QFile::Text))
        editor->setHtml(file.readAll());

    QTextBlock text_block = text_document->begin();
    while (text_block.isValid() )
    {
        qDebug() << text_block.text() << CalculateLineHeight(text_block);
        text_block = text_block.next();
    }
    window->setCentralWidget(editor);
    window->show();
    return app.exec();
}

int CalculateLineHeight(QTextBlock text_block)
{
    QList<QTextLayout::FormatRange> text_block_format_ranges;

    // Gather the format ranges for each fragment of the text block.
    for (QTextBlock::Iterator fragment_it = text_block.begin(); !(fragment_it.atEnd()); ++fragment_it)
    {
        QTextFragment fragment = fragment_it.fragment();

        QTextCharFormat fragment_format = fragment.charFormat();
        QTextLayout::FormatRange text_block_format_range;
        text_block_format_range.format = fragment_format;
        text_block_format_range.start = fragment.position();
        text_block_format_range.length = fragment.length();

        text_block_format_ranges << text_block_format_range;
    }
    // Create text layout
    QTextLayout text_layout(text_block.text());
    text_layout.setAdditionalFormats( text_block_format_ranges );
    text_layout.beginLayout();
    QTextLine line = text_layout.createLine();
    text_layout.endLayout();

    return text_layout.boundingRect().height();
}

这是test.html:

<!DOCTYPE html>
<html>
<head>
<style>
.consolas{
    font-family: "Consolas";
    font-size:14pt;
    background-color: cyan; 
}
.arial{
    font-family: "Arial";
    font-size:14pt;
    background-color: cyan; 
}
</style>
</head>

<body>
<p><span class="consolas">E</span></p>
<p><span class="arial">E</span></p>
<p><span class="consolas">E</span><span class="arial">E</span></p>
</body>
</html>

窗口显示(我的注释指示LineHeights):

enter image description here

但我得到以下控制台输出:

"E" 22
"E" 13
"EE" 13

所以,它显然在第一行上正确计算它,但后续行不能正确计算它(第二行s / b 23,第三行s / b 24)。我怀疑我的问题在于我如何处理文本布局。

1 个答案:

答案 0 :(得分:1)

这就是诀窍:

int CalculateLineHeight(QTextBlock text_block)
{
    QList<QTextLayout::FormatRange> text_block_format_ranges;

    // Gather the format ranges for each fragment of the text block.
    for (QTextBlock::Iterator fragment_it = text_block.begin(); !(fragment_it.atEnd()); ++fragment_it)
    {
        QTextFragment fragment = fragment_it.fragment();

        QTextCharFormat fragment_format = fragment.charFormat();
        QTextLayout::FormatRange text_block_format_range;
        text_block_format_range.format = fragment_format;
        // fragment.position = position within the document whereas
        // .start = position within the text block. Therefore, need
        // to subtract out the text block's starting position within the document.
        text_block_format_range.start = fragment.position()
                                       - text_block.position();
        text_block_format_range.length = fragment.length();

        text_block_format_ranges << text_block_format_range;
    }

    // Create text layout
    QTextLayout text_layout(text_block.text());
    text_layout.setAdditionalFormats( text_block_format_ranges );
    text_layout.beginLayout();
    QTextLine line = text_layout.createLine();
    text_layout.endLayout();                    
    line.setLeadingIncluded(true);                  // Need to include the leading
    return line.height();