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是這樣的