QWebEngineView-加载> 2mb内容 [英] QWebEngineView - loading of > 2mb content

查看:454
本文介绍了QWebEngineView-加载> 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.

使用文件地址并指向本地文件.但是,这是一个非常糟糕的解决方案,因为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..

然后重新添加各自的包含内容

And rember to add the respective includes

#include <QWebEngineUrlRequestJob>
#include <QWebEngineUrlSchemeHandler>
#include <QBuffer>

这篇关于QWebEngineView-加载&gt; 2mb内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆