1. 程式人生 > 其它 >Qt qml webview載入本地html

Qt qml webview載入本地html

技術標籤:Qtandroidios

Qt qml webview載入本地html

用qml做移動專案也有段時間了,今天分享一下qml的webview載入本地html吧

webview載入本地html,在官方並沒有明確說明

在andrioid下webview是這樣的
WebView
{
id:web
anchors.centerIn: parent
anchors.fill: parent
url:“file:///android_asset/data/baidumap.html”
}
這個html檔案需要放到android/assets資料夾下

webview在iOS中載入本地html需要費些周折

首先需要在cpp裡取到html的位置,並且宣告這個url
QString initialUrl = “file:/” +QCoreApplication::applicationDirPath()+"/baidumap.html";
engine.rootContext()->setContextProperty(QStringLiteral(“initialUrl”),QUrl::fromUserInput(initialUrl));

如果網頁內有http請求,一定要用https

WebView
{
id:web
anchors.centerIn: parent
anchors.fill: parent

url:Qt.platform.os === “android”?“file:///android_asset/data/map.html”
:initialUrl;
onLoadProgressChanged:
{
if(web.loadProgress == 100)
{
var queryFun = “loadMap();”
web.runJavaScript(queryFun, function(result) { console.log(result); });
}
}
}
Xcode是這樣的
在這裡插入圖片描述