3

给定QTextDocument具有特定宽度和高度的 a ,有没有办法在给定页码的情况下获取给定页面的内容(如果页面上有图像,则为纯文本 + 图像 URL)?

这是我想要实现的示例:

QString getTextForPage(int pageNumber); // this is the function I'd like to have
QString getURLForPage(int pageNumber); // this is the function I'd like to have

QString html = R"(
<!DOCTYPE html>
<html>
<head>
  <meta charset="UTF-8">
  <style>
    .summary {page-break-after: always}
  </style>
  <title>title</title>
</head>
  <p class="summary">This is a short summary which would fit into a page</p>
  <p><img src="www.example.com/test.png" height="100" width="200"></p>
  <p><img src"www.example.com/test2.png>" height="50" width="10"</p>
  <p>This is a short text which should fit into a page<p>
<body>

</body>
</html>
)"

const auto width = 100;
const auto height = 200;
auto textDoc = new QTextDocument();
textDoc->setHtml(html);
textDoc->setPageSize(QSizeF {wide, height});
textDoc->setDocumentMargin(0);

for (auto curPageNum = 1; curPageNum <= textDoc->pageCount(); ++curPageNum) {
    qDebug() << "current page: " << curPageNum;
    qDebug() << getTextForPage(curPageNum);
    qDebug() << getURLForPage(curPageNum);
}

这应该打印:

1
This is a short summary which would fit into a page
(empty string as there is no URL)
2
(empty string as the there is no text on the page)
www.example.com/test.png
3
This is a short text which should fit into a page
www.example.com/test2.png

通常,p 标签中的文本可以跨越多个页面,并且图像保证最多跨越一页,以防万一。

4

1 回答 1

0

无法将特定页面的内容作为“纯文本 + 图像 URL”获取。Qt 不支持。特别是当您将内容设置为 html 时,Qt 不会为您提取子 html 文档...

QPrinter通过使用并请求仅打印特定页面,您可以获得图像(或 pdf)形式的内容。如果这对您来说是一个可以接受的解决方案,我可以发布代码来展示如何做到这一点。

于 2018-03-18T09:46:27.617 回答