Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. QML and Qt Quick
  4. How to call qml function from html side
Qt 6.11 is out! See what's new in the release blog

How to call qml function from html side

Scheduled Pinned Locked Moved Unsolved QML and Qt Quick
3 Posts 2 Posters 1.5k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • B Offline
    B Offline
    bartalke
    wrote on last edited by
    #1

    Hi,
    I want to invoke form HTML a someFoo("someStr") function wich 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

    D 1 Reply Last reply
    1
    • D Offline
      D Offline
      DBoosalis
      wrote on last edited by
      #2

      Hard to say, as you don't provide all the code. I have used webchannel with Qt 5.9 so know it works. Your code looks right
      Do you have a "import QtWebChannel" in your QML. How about moving the WebChangel declaration out of your WebEngineView
      and/or declaring it like this:

      WebChannel {
      id:wc
      registeredObjects: [mapController]
      }

      WebEngineView {
      ...
      webChannel: wc
      }

      1 Reply Last reply
      0
      • B bartalke

        Hi,
        I want to invoke form HTML a someFoo("someStr") function wich 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

        D Offline
        D Offline
        DBoosalis
        wrote on last edited by
        #3

        @bartalke
        One more thing , since our problem seems to be on the html side. Try removing the qrc: from the javascript.
        src="qwebchannel.js"

        1 Reply Last reply
        0

        • Login

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • Users
        • Groups
        • Search
        • Get Qt Extensions
        • Unsolved