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 内容
				
        
 
            
        - Css:将嵌套元素定位在父元素边界之外一点 2022-09-07
 - Flexslider 箭头未正确显示 2022-01-01
 - addEventListener 在 IE 11 中不起作用 2022-01-01
 - 400或500级别的HTTP响应 2022-01-01
 - Quasar 2+Apollo:错误:找不到ID为默认的Apollo客户端。如果您在组件设置之外,请使用ProvideApolloClient() 2022-01-01
 - 如何使用 JSON 格式的 jQuery AJAX 从 .cfm 页面输出查 2022-01-01
 - Fetch API 如何获取响应体? 2022-01-01
 - 使用RSelum从网站(报纸档案)中抓取多个网页 2022-09-06
 - CSS媒体查询(最大高度)不起作用,但为什么? 2022-01-01
 - 失败的 Canvas 360 jquery 插件 2022-01-01
 
						
						
						
						
						