QWebEngineView - loading of gt; 2mb content(QWebEngineView - 加载gt;2mb 内容)
问题描述
因此,使用 PyQt5 的 QWebEngineView 以及 .setHTML 和 .setContent 方法有 2 MB 的大小限制.在谷歌上寻找解决方案时,我发现了两种方法:
So, using PyQt5's QWebEngineView and the .setHTML and .setContent methods have a 2 MB size limitation. When googling for solutions around this, I found two methods:
使用 SimpleHTTPServer 提供文件.然而,这被公司使用的防火墙破坏了.
Use SimpleHTTPServer to serve the file. This however gets nuked by a firewall employed in the company.
使用文件 URL 并指向本地文件.然而,这是一个相当糟糕的解决方案,因为 HTML 包含机密数据,在任何情况下我都不能将其留在硬盘上.
Use File Urls and point to local files. This however is a rather bad solution, as the HTML contains confidential data and I can't leave it on the harddrive, under any circumstance.
我目前看到的最佳解决方案是使用文件 url,并在程序退出/当 loadCompleted 报告完成时删除文件,以先到者为准.
The best solution I currently see is to use file urls, and get rid of the file on program exit/when loadCompleted reports it is done, whichever comes first.
但这不是一个很好的解决方案,我想问一下是否有一个我忽略的解决方案会更好?
This is however not a great solution and I wanted to ask if there is a solution I'm overlooking that would be better?
推荐答案
为什么不通过自定义 url 方案处理程序加载/链接大部分内容?
Why don't you load/link most of the content through a custom url scheme handler?
webEngineView->page()->profile()->installUrlSchemeHandler("app", new UrlSchemeHandler(e));
class UrlSchemeHandler : public QWebEngineUrlSchemeHandler
{ Q_OBJECT
public:
void requestStarted(QWebEngineUrlRequestJob *request) {
QUrl url = request->requestUrl();
QString filePath = url.path().mid(1);
// get the data for this url
QByteArray data = ..
//
if (!data.isEmpty())
{
QMimeDatabase db;
QString contentType = db.mimeTypeForFileNameAndData(filePath,data).name();
QBuffer *buffer = new QBuffer();
buffer->open(QIODevice::WriteOnly);
buffer->write(data);
buffer->close();
connect(request, SIGNAL(destroyed()), buffer, SLOT(deleteLater()));
request->reply(contentType.toUtf8(), buffer);
} else {
request->fail(QWebEngineUrlRequestJob::UrlNotFound);
}
}
};
然后您可以通过 webEngineView->load(new QUrl("app://start.html"));
内部的所有相对路径也将转发到您的 UrlSchemeHandler..
All relative pathes from inside will also be forwarded to your UrlSchemeHandler..
记得添加相应的包含
#include <QWebEngineUrlRequestJob>
#include <QWebEngineUrlSchemeHandler>
#include <QBuffer>
这篇关于QWebEngineView - 加载>2mb 内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:QWebEngineView - 加载>2mb 内容
基础教程推荐
- 如何使用JIT在顺风css中使用布局变体? 2022-01-01
- html表格如何通过更改悬停边框来突出显示列? 2022-01-01
- 我可以在浏览器中与Babel一起使用ES模块,而不捆绑我的代码吗? 2022-01-01
- Electron 将 Node.js 和 Chromium 上下文结合起来意味着 2022-01-01
- Vue 3 – <过渡>渲染不能动画的非元素根节点 2022-01-01
- 如何使用TypeScrip将固定承诺数组中的项设置为可选 2022-01-01
- 用于 Twitter 小部件宽度的 HTML/CSS 2022-01-01
- 自定义 XMLHttpRequest.prototype.open 2022-01-01
- Chart.js 在线性图表上拖动点 2022-01-01
- 直接将值设置为滑块 2022-01-01