如何从html端调用qml函数

时间:2017-06-16 20:48:57

标签: javascript html qt qml qtquick2

我想调用HTML格式的 someFoo(" someStr")函数,它位于QML中,如下所示:

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";

        function someFoo(msg) {
            console.log(msg);
        }
    }

    WebEngineView {
        id: view
        anchors.fill: parent
        url: "myHtml.html"

        WebChannel {
           id: webChannel
           registeredObjects: [myQmlObj]
        }
    }
}

HTML

<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>

.pro 文件包含

QT += core gui network webenginewidgets webchannel

但是当我尝试它时,我收到以下错误:

js:未捕获的ReferenceError:未定义qt

有人知道我做错了什么吗?我正在使用Qt 5.9

0 个答案:

没有答案