main.cpphtml
QString tmploc = QStandardPaths::writableLocation(QStandardPaths::GenericDataLocation);
QDir tmpdir(tmploc + "/my_little_project");web
QDirIterator it(":", QDirIterator::Subdirectories); bool isHtml = false; while (it.hasNext()) { QString tmpfile; tmpfile = it.next(); isHtml = tmpfile.contains("html",Qt::CaseInsensitive); if (QFileInfo(tmpfile).isFile() && (isHtml) ) { QFileInfo file = QFileInfo(tmpdir.absolutePath() + tmpfile.right(tmpfile.size()-1)); file.dir().mkpath("."); // create full path if necessary QFile::remove(file.absoluteFilePath()); // remove previous file to make sure we have the latest version QFile::copy(tmpfile, file.absoluteFilePath()); break; } } ctxt->setContextProperty(QStringLiteral("baseUrl"), QFileInfo(tmpdir.absolutePath() + "/index1.html").absoluteFilePath());
web.qml中
//import QtQuick 2.0
import QtQuick 2.2
import QtQuick.Layouts 1.1
import QtQuick.Controls 1.2 as QuickControls
import QtWebView 1.0ui
Item{
signal changeUrl(string msg)
property string webParams
property alias weburl: mywebview.url
WebView{
id:mywebview
width:parent.width
height: parent.height
// url: baseUrl
// url:"file:///storage/sdcard0/my_little_project/index1.html"
url:"file:///"+baseUrl //這裏必須這麼寫,不然有問題
onUrlChanged: {
changeUrl(mywebview.url+"")
webParams = mywebview.url
console.log("~~~~~~~~~~url:"+baseUrl)
}
}url
}code