gpt4 book ai didi

c++ - QTextDocument:获取特定页面的内容

转载 作者:太空狗 更新时间:2023-10-29 23:11:47 28 4
gpt4 key购买 nike

给定一个具有特定宽度和高度的QTextDocument,有没有办法获取给定页面的内容(如果页面上有图像,则为纯文本+图像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 标签中的文本可以跨越多个页面,而图像保证最多跨越一页,以防万一。

最佳答案

无法以“明文+图片URL”的方式获取特定页面的内容。 Qt 不支持那个。特别是当您将内容设置为 html 时,Qt 不会为您提取子 html 文档...

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

关于c++ - QTextDocument:获取特定页面的内容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49282942/

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