Javascript 如何从html端调用qml函数
我想调用位于QML中的表单HTML someFoo(“someStr”)函数,如下所示: QMLJavascript 如何从html端调用qml函数,javascript,html,qt,qml,qtquick2,Javascript,Html,Qt,Qml,Qtquick2,我想调用位于QML中的表单HTML someFoo(“someStr”)函数,如下所示: 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 WebC
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";
function someFoo(msg) {
console.log(msg);
}
}
WebEngineView {
id: view
anchors.fill: parent
url: "myHtml.html"
WebChannel {
id: webChannel
registeredObjects: [myQmlObj]
}
}
}
<script type="text/javascript" src="qrc:///qtwebchannel/qwebchannel.js"></script>
<script type="text/javascript">
new QWebChannel(qt.webChannelTransport, function (channel) {
var myQmlObj = channel.objects.myQmlObj;
myQmlObj.someFoo("blabla");
});
</script>
HTML
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";
function someFoo(msg) {
console.log(msg);
}
}
WebEngineView {
id: view
anchors.fill: parent
url: "myHtml.html"
WebChannel {
id: webChannel
registeredObjects: [myQmlObj]
}
}
}
<script type="text/javascript" src="qrc:///qtwebchannel/qwebchannel.js"></script>
<script type="text/javascript">
new QWebChannel(qt.webChannelTransport, function (channel) {
var myQmlObj = channel.objects.myQmlObj;
myQmlObj.someFoo("blabla");
});
</script>
但当我尝试时,我得到以下错误:
js:uncaughtreferenceerror:qt未定义
有人知道我做错了什么吗?我正在使用Qt 5.9看看。您只需要从qmlth的Qt对象调用QML函数,这非常有用,现在它可以工作了,非常感谢!看一看。您只需要从qmlth的Qt对象调用QML函数,这非常有用,现在它可以工作了,非常感谢!