gpt4 book ai didi

javascript - 如果从 qresources 加载 html 文件,QWebView 不会加载任何外部资源

转载 作者:塔克拉玛干 更新时间:2023-11-02 23:29:04 26 4
gpt4 key购买 nike

如标题中所述,我的问题是 qwebview 无法正确加载位于我的资源中的 html 文件。如果我从资源外部加载它作为普通本地文件,它会完美地加载它。但这对我来说不是一个选择。我想将该文件与应用程序捆绑在一起。

编辑:顺便说一句,我说的是来自网络的外部资源。 (例如 http://host.org/somejavascript.js)感谢您的帮助

最佳答案

请看一下
的第二个参数void QWebView::setHtml (const QString & html, const QUrl & baseUrl = QUrl())根据documentation :

External objects such as stylesheets or images referenced in the HTML document are located relative to baseUrl.

下面是适合我的代码。

#include <QtCore/QFile>
#include <QtCore/QUrl>
#include <QtGui/QApplication>
#include <QtGui/QMainWindow>
#include <QtWebKit/QWebView>


int main(int argc, char *argv[])
{
QApplication app(argc, argv);

QMainWindow window;
QWebView webview(&window);

QFile source(":/google.com.html");
source.open(QIODevice::ReadOnly);
webview.setHtml(QString::fromUtf8(source.readAll().constData()), QUrl("http://google.com"));
window.setCentralWidget(&webview);
window.show();

return app.exec();
}

关于javascript - 如果从 qresources 加载 html 文件,QWebView 不会加载任何外部资源,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5126294/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com