如何确定Qt文档的线的渲染高度 [英] How to determine the rendered height of the lines of a Qt document

查看:454
本文介绍了如何确定Qt文档的线的渲染高度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Windows 7 SP1

MSVS 2010

Qt 4.8.4

Windows 7 SP1
MSVS 2010
Qt 4.8.4

我试图确定Qt文档的每行(准确的文本块)的渲染高度。文档具有富文本,因此每个文本块可能具有片段。假设没有换行,所以每个文本块都是一行。为了简化操作,这里是我想要做的:

I am trying to determine what the rendered heights of each line (text block to be exact) of a Qt document are. The document has rich text, so each text block may have fragments. Assume no word wrapping so each text block is a line. To simplify things, here is what I want to do:

 #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:

This is 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):

The window displays (with my annotations indicating the LineHeights):

>

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

But I get the following console output:

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

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

So, it apparently calculates it properly on the first line, but subsequent lines it does not calculate it properly (2nd line s/b 23, 3rd s/b 24). I suspect my problem lies in how I am handling the text layouts.

推荐答案


$ b

This did the trick:

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

这篇关于如何确定Qt文档的线的渲染高度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆