从 Qt 5 WebView 获取日志输出?

Getting log output from Qt 5 WebView?

我正在尝试从 Qt 5 WebView 网页获取控制台日志输出,但我不知道该怎么做。 也许你们中的一些人可以帮助我?

我尝试启用应该在您右键单击网页时显示的 Web 检查器,但是当我这样做时没有任何反应。我已经通过将环境变量 QTWEBKIT_INSPECTOR_SERVER 设置为 1111 来设置检查器端口(在 1111 上)。我能够获得一个包含此内容的页面:

Inspectable web views

LOG TEST [http://MY_LAN_IP:8880/logtest.html]

但是当我点击 link 时,我得到了这个错误:

WebSocket connection to 'ws://localhost:1111/devtools/page/1' failed: Error during WebSocket handshake: Sent non-empty 'Sec-WebSocket-Protocol' header but no response was received
inspector.js:341 Event {clipboardData: undefined, path: NodeList[0], cancelBubble: false, returnValue: true, srcElement: WebSocket…}
View.js:363 Synchronous XMLHttpRequest on the main thread is deprecated because of its detrimental effects to the end user's experience. For more help, check http://xhr.spec.whatwg.org/.

出于演示目的,我在本地网络服务器上有一个网页,如下所示:

<!DOCTYPE html>
<html>
    <head>
        <title>LOG TEST</title>
    </head>
    <body>
        <h1>Logging to console...</h1>
        <script>
            setInterval(function() {
                console.log("This is a log message");
                console.error("This is an error message");
            }, 1000);
        </script>
    </body>
</html>

QML 文件如下所示:

import QtQuick 2.2
import QtQuick.Window 2.1
import QtWebKit 3.0
import QtWebKit.experimental 1.0

Window {
    visible: true
    width: 360
    height: 360

    WebView {
        url: "http://MY_LAN_IP:8880/logtest.html"
        anchors.fill: parent
        experimental.preferences.developerExtrasEnabled: true
        experimental.preferences.navigatorQtObjectEnabled: false

    }
}

关注 this link 我成功地从嵌入 QML WebView 的网页中显示了我的日志。

在你的 post 中,你离目标很近了。

你必须:

  1. 将运行时环境变量 QTWEBKIT_INSPECTOR_SERVER 设置为您想要的任何值(为什么不是 1111)。
  2. 在QML文件中导入QtWebKit.experimental1.0
  3. 并在 webview 属性中添加:experimental.preferences.developerExtrasEnabled: true

然后,当您启动应用程序时,您必须具有以下输出跟踪:

Inspector server started successfully. Try pointing a WebKit browser to http://127.0.0.1:1111

要调试您的页面,请打开您最喜欢的网络浏览器并转到 http://127.0.0.1:1111,即可完成!

因此界面发生了变化,在旧的 QtWebKit 中,您必须右键单击 Web 视图才能调试页面,现在您必须通过 Web 浏览器连接到服务器。