QWebEngineView - 加载>2mb 内容

QWebEngineView - loading of gt; 2mb content(QWebEngineView - 加载gt;2mb 内容)

本文介绍了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.

使用文件 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 - 加载&gt;2mb 内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:QWebEngineView - 加载&gt;2mb 内容

基础教程推荐