QtWebEngine: “Not allowed to load local resource” for iframe, how to disable web security?

青春壹個敷衍的年華 提交于 2019-12-05 12:13:20

this Qt forum link help you . you should pass argument to application "--disable-web-security" https://forum.qt.io/topic/60691/not-allowed-to-load-local-resource-for-iframe-how-to-disable-web-security/4

If you need to load local resource(s) by WebEngine, then you need to pass --disable-web-security argument to QApplication, e.g.:

char ARG_DISABLE_WEB_SECURITY[] = "--disable-web-security";
int newArgc = argc+1+1;
char** newArgv = new char*[newArgc];
for(int i=0; i<argc; i++) {
    newArgv[i] = argv[i];
}
newArgv[argc] = ARG_DISABLE_WEB_SECURITY;
newArgv[argc+1] = nullptr;

QApplication myApplication(newArgc, newArgv);

Another option is to load the original page from the filesystem as well. I was having issues loading images from Qt's resource system, so I subclassed QWebEngineView and created this function:

void WebEngineView::setLocalHtml(const QString &html)
{
    if(html.isEmpty())
    {
        setHtml(QString());
        return;
    }

    // Save html to a local file
    QString filePath;
    {
        QTemporaryFile tempFile(QDir::toNativeSeparators(QDir::tempPath() + "/ehr_temp.XXXXXX.html"));
        tempFile.setAutoRemove(false);
        tempFile.open();
        QTextStream out(&tempFile);
        out << html;

        filePath = tempFile.fileName();
    }

    // delete the file after it has been loaded
    QMetaObject::Connection * const conn = new QMetaObject::Connection;
    *conn = connect(this, &WebEngineView::loadFinished, [filePath, conn](){
        disconnect(*conn);
        delete conn;

        QFile::remove(filePath);
    });

    load(QUrl::fromLocalFile(filePath));
}

Since the main page is a local file as well, this gets around the CORS security problem.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!