1

I want to invoke form HTML a someFoo("someStr") function which is located in QML as follows:

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

QT += core gui network webenginewidgets webchannel

but when I try it I get the following error:

js: Uncaught ReferenceError: qt is not defined

Does anybody have an idea what I am doing wrong? I am using Qt 5.9.

2
  • 1
    Have a look here. You just need to call QML function from Qt Object of QML Commented Jun 18, 2017 at 9:56
  • That was very helpful, now it works thanks a lot ! Commented Jun 18, 2017 at 11:53

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.