如何确定 Qt 文档行的渲染高度
Posted
技术标签:
【中文标题】如何确定 Qt 文档行的渲染高度【英文标题】:How to determine the rendered height of the lines of a Qt document 【发布时间】:2013-02-09 22:00:46 【问题描述】: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):
但我得到以下控制台输出:
"E" 22
"E" 13
"EE" 13
因此,它显然在第一行正确计算它,但后续行它没有正确计算它(第 2 行 s/b 23,第 3 s/b 24)。我怀疑我的问题在于我如何处理文本布局。
【问题讨论】:
【参考方案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();
【讨论】:
以上是关于如何确定 Qt 文档行的渲染高度的主要内容,如果未能解决你的问题,请参考以下文章
如何快速更改 UISearchController 行的高度
在容器 Kendo 窗口调整大小时动态更改 Kendo Grid 行的高度