2015-02-10 128 views
1

我想从Qt 5 WebView网页获取控制台日志输出,但我无法找到如何去做。 也许有些你可以帮助我吗?从Qt 5 WebView获取日志输出?

我试图启用当您右键单击网页时显示的Web检查器,但是当我这样做时没有任何反应。我已经通过了环境变量QTWEBKIT_INSPECTOR_SERVER设置为1111设立督察端口(在1111),我能够得到一个网页,有这个就可以了:

Inspectable web views 

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

但是当我点击我得到这个链接错误:

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/. 

为了演示我有一个看起来像这样的本地Web服务器上的网页:

<!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 

    } 
} 

回答

0

正在关注this链接我成功地显示了嵌入在QML WebView中的网页中的日志。

在你的文章中,你真的接近了目标。

您必须:

  1. 设置为任何你想要的运行时环境变量QTWEBKIT_INSPECTOR_SERVER(为什么不1111)。
  2. 在QML文件导入QtWebKit.experimental 1.0
  3. 和web视图性的判定补充:experimental.preferences.developerExtrasEnabled:真

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

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浏览器。