bladzio
bladzio

Reputation: 424

How to call HTML function from QML

I want to call reload function in HTML from QML. I can call a QML function from HTML as in following code.

QML file:

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

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

Like i can call someFoo() from HTML, But i can't call reload function of HTML from my qml.

Upvotes: 1

Views: 1138

Answers (1)

SourabhKus
SourabhKus

Reputation: 828

You can call runJavaScript function as following

function callHtmlFunction(){
    view.runJavaScript("reload("+ color +")");
}

Upvotes: 1

Related Questions