1
我想調用從QML的HTML中重新加載函數。我可以在HTML中調用QML函數,如下面的代碼所示。如何從QML調用HTML函數
QML文件:
import QtQuick 2.5
import QtQuick.Window 2.2
import QtWebEngine 1.0
import QtWebChannel 1.0
Window {
visible: true
width: 640
height: 480
QtObject {
id: myQmlObj
WebChannel.id: "myQmlObj";
signal someSignal(string msg);
function someFoo(msg) {
console.log(msg);
}
}
WebEngineView {
id: view
anchors.fill: parent
url: "myHtml.html"
WebChannel {
id: webChannel
registeredObjects: [myQmlObj]
}
}
}
HTML文件:
<html>
<body>
<script type="text/javascript" src="qrc:///qtwebchannel/qwebchannel.js"></script>
<script type="text/javascript">
var color = 1;
var webChannel;
function init() {
color = 5;
};
function createChannel(lat) {
new QWebChannel(qt.webChannelTransport, function (channel) {
webChannel = channel.objects.myQmlObj;
webChannel.someSignal.connect("someSignal");
webChannel.someFoo("someFoo");
});
}
function reload(id) {
color = id;
}
</script>
<script src="someUrl?callback=init"></script>
</body>
</html>
就像我可以從HTML調用someFoo(),但我無法從我的QML調用HTML的裝載功能。