QML WebEngineView简单用法和常用接口
前言
WebEngineView用于在 QML 界面中加載一個(gè)網(wǎng)頁(yè),該組件和QWebEngineView類對(duì)應(yīng),接口都是一樣的,只是一個(gè)在 QML 中使用,一個(gè)是在 C++中使用,這里只根據(jù)在平常的使用中總結(jié)的簡(jiǎn)單的WebEngineView用法。
正文
簡(jiǎn)單用法
WebEngineView的使用非常簡(jiǎn)單,這里摘一段 Qt 幫助文檔中的示例:
import QtQuick 2.0import QtQuick.Window 2.0import QtWebEngine 1.0Window {width: 1024height: 750visible: trueWebEngineView {id:webviewanchors.fill: parenturl: "http://www.www.baidu.com"}}如上示例,只需要指定 url,即可自動(dòng)加載網(wǎng),但是如果僅僅就寫成這樣,會(huì)出現(xiàn)一個(gè)問題,就是只能加載當(dāng)前這個(gè)網(wǎng)頁(yè),如果在網(wǎng)頁(yè)里面還想再次點(diǎn)擊其他鏈接,這樣是不會(huì)相應(yīng)的,那么該怎么實(shí)現(xiàn)這個(gè)功能呢,加上以下這句:
onNewViewRequested: request.openIn(webview)這里的newViewRequested是一個(gè)信號(hào),來(lái)看一下 Qt 幫助文檔中對(duì)這個(gè)信號(hào)的介紹:
This signal is emitted when a page load is requested to happen in a separate web engine view. This can either be because the current page requested it explicitly through a JavaScript call to window.open, or because the user clicked on a link while holding Shift, Ctrl, or a built-in combination that triggers the page to open in a new window.
幫助文檔寫的很清楚,這里就不過(guò)多介紹了,實(shí)現(xiàn)方式就是綁定過(guò)該信號(hào),然后加上一句:request.openIn(webview)
網(wǎng)頁(yè)中的視頻不能加載
通過(guò)以上方式加載網(wǎng)頁(yè)后,如果網(wǎng)頁(yè)中包含視頻或音頻之類的元素,是不能正常播放的,這是因?yàn)闆]有默認(rèn)運(yùn)行插件,如 flash player,設(shè)置方法如下:
webview.settings.pluginsEnabled:true官網(wǎng)介紹如下:
*Enables support for Pepper plugins, such as the Flash player.
Disabled by default.*
除了以上設(shè)置,WebEngineSettings中還有很多其他屬性可以設(shè)置,具體查看幫助文檔。
緩存路徑,清除緩存
加載網(wǎng)頁(yè)會(huì)有緩存產(chǎn)生,那么緩存的路徑如下:
webview.profile.cachePath清理緩存
webview.profile.clearHttpCache()注意,清理緩存的接口在QtWebEngine 1.3中才有。
還有一個(gè)接口有必要說(shuō)一下,重新加載網(wǎng)頁(yè)內(nèi)容可以用 reload()函數(shù),有一個(gè)和它類似的接口叫
官網(wǎng)的介紹如下:
*Reloads the current page, ignoring any cached content.
This QML method was introduced in QtWebEngine 1.1.*
這個(gè)接口的作用就是跳過(guò)緩存然后重新加載網(wǎng)頁(yè),如果網(wǎng)頁(yè)有改動(dòng),又不想去清除緩存,那么可以通過(guò)該接口來(lái)重新加載網(wǎng)頁(yè)。
OK,以上是最近在項(xiàng)目中遇到的關(guān)于 QML 加載網(wǎng)頁(yè)相關(guān)的問題,做個(gè)簡(jiǎn)單的記錄。
總結(jié)
以上是生活随笔為你收集整理的QML WebEngineView简单用法和常用接口的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Qt 通过鼠标移动窗口
- 下一篇: Qt UDP 广播简单示例