gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-04 11:59:20 33 4
gpt4 key购买 nike

Windows 7 SP1
MSVS 2010
Qt 4.8.4

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

 #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

因此,它显然在第一行计算正确,但后续行计算不正确(第 2 行 s/b 23、第 3 行 s/b 24)。我怀疑我的问题在于我如何处理文本布局。

最佳答案

这成功了:

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

关于c++ - 如何确定Qt文档行的渲染高度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14792074/

33 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com