本文介绍了QtWebEngine:“不允许加载本地资源”为iframe,如何禁用网络安全?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在将我的应用程序从WebKit移植到WebEngine(似乎对于渲染angular-basad html来说更好)。
我面临的问题是,我无法启用QtWebEngine加载本地iframe,尽管事实上我已经设置了我发现的所有可能的设置:



代码from mainwindow.cpp

  view-> page() - > settings() - > setAttribute(QWebEngineSettings :: LocalContentCanAccessFileUrls,true); 
view-> page() - > settings() - > setAttribute(QWebEngineSettings :: LocalContentCanAccessRemoteUrls,true);
view-> page() - > settings() - > setAttribute(QWebEngineSettings :: LocalStorageEnabled,true);

view-> settings() - > setAttribute(QWebEngineSettings :: LocalContentCanAccessFileUrls,true);
view-> settings() - > setAttribute(QWebEngineSettings :: LocalContentCanAccessRemoteUrls,true);
view-> settings() - > setAttribute(QWebEngineSettings :: LocalStorageEnabled,true);

最简单的例子就是采用基于WebEngine的FancyBrowser(\Examples\Qt-5.4\ webenginewidgets\fancybrowser),并尝试加载如下的本地html文件:

Index.html:

 < HTML> 
< head>
< title>您好< / title>
< / head>
< body>
这是一个页面
a简单页面
< / body>
< / html>

some_iframe.html:

 <!DOCTYPE html> 
< html lang =en>
< head>
< meta charset =utf-8>
< title> La-la-la< / title>
< / head>
< body>
Lalala
< / body>
< / html>

如果将env var QTWEBENGINE_REMOTE_DEBUGGING设置为某个端口,则可以打开127.0.0.1:port并查看在控制台中出现这个错误:

 不允许加载本地资源。 

我真的不知道现在如何解决这个问题...应该有一些方法可以通过到WebEngine的东西像--disable-web-security...

感谢您的帮助!

解决方案

这个Qt论坛链接帮助你。
您应该将参数传递给应用程序--disable-web-security

I'm porting my application from WebKit to WebEngine (seems that one is much better for rendering angular-basad html).I faced with problem that i can't enable QtWebEngine to load local iframe, despite the fact that i've setup all possible settings that i found:

Code from mainwindow.cpp

view->page()->settings()->setAttribute(QWebEngineSettings::LocalContentCanAccessFileUrls, true);
view->page()->settings()->setAttribute(QWebEngineSettings::LocalContentCanAccessRemoteUrls, true);
view->page()->settings()->setAttribute(QWebEngineSettings::LocalStorageEnabled, true);

view->settings()->setAttribute(QWebEngineSettings::LocalContentCanAccessFileUrls, true);
view->settings()->setAttribute(QWebEngineSettings::LocalContentCanAccessRemoteUrls, true);
view->settings()->setAttribute(QWebEngineSettings::LocalStorageEnabled, true);

The easiest example is to take WebEngine-based FancyBrowser (\Examples\Qt-5.4\webenginewidgets\fancybrowser) and try to load in it local html file like this:

Index.html:

<html>
<head>
    <title>Hi there</title>
</head>
<body>
    This is a page
    a simple page
    <iframe id="some_idrame" width="0" height="0" style="border: none" src="some_iframe.html" name="target" sandbox="allow-scripts"></iframe>
</body>
</html>

some_iframe.html:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="utf-8">
    <title>La-la-la</title>
</head>
<body>
    Lalala
</body>
</html>

If you setup env var QTWEBENGINE_REMOTE_DEBUGGING to some port, then you can open 127.0.0.1:port and see in console this error:

"Not allowed to load local resource".

I really have no idea how to solve this problem now... there should be some way to pass to WebEngine something like "--disable-web-security"...

Thanks for any help!

解决方案

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

这篇关于QtWebEngine:“不允许加载本地资源”为iframe,如何禁用网络安全?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-26 08:04