Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. QtWebEngine
  4. "qt is undefined " when I declare QWebChannel
Forum Updated to NodeBB v4.3 + New Features

"qt is undefined " when I declare QWebChannel

Scheduled Pinned Locked Moved Unsolved QtWebEngine
4 Posts 4 Posters 3.9k Views 3 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.
  • M Offline
    M Offline
    Mason1024
    wrote on last edited by Mason1024
    #1

    Hello everyone.
    I tried to use QWebEngineView with Qt 5.7.0 on Windows, I found it dose not work to invoke c++ function from javascript

        QWebChannel *channel{new QWebChannel(this)};
        channel->registerObject(QStringLiteral("VisJsApi"), jsApi);
        channel->registerObject(QStringLiteral("VPntr"), pjso);
    
        webView->page()->setWebChannel(channel);
    
    document.addEventListener("DOMContentLoaded", function() {
        new QWebChannel(qt.webChannelTransport, function(channel) {
            window.VisJsApi = channel.objects.VisJsApi;
            window.VPntr = channel.objects.VPntr;
        });
    });
    

    when I use textQtWebchannel to test.

    function textQtWebchannel() {
    if (typeof QWebChannel != 'undefined') {
            alert("QWebChannel on");
        } else {
            alert("QWebChannel off");
        }
    
        if (typeof navigator != 'undefined') {
            alert("navigator on");
    
            if (typeof navigator.qtWebChannelTransport != 'undefined') {
                alert("navigator.qtWebChannelTransport on");
            } else {
                alert("navigator.qtWebChannelTransport off");
            }
        } else {
            alert("navigator off");
        }
    
        if (typeof qt != 'undefined') {
            alert("qt on");
    
            if (typeof qt.webChannelTransport != 'undefined') {
                alert("qt.webChannelTransport on");
    
                new QWebChannel(qt.webChannelTransport, function(channel) {
                    window.VisJsApi = channel.objects.VisJsApi;
                    window.VPntr = channel.objects.VPntr;
                });
    
            } else {
                alert("qt.webChannelTransport off");
            }
        } else {
            alert("qt off");
        }
    };
    

    what i finially find it print out:

    QWebChannel on
    navigator on
    navigator.qtWebChannelTransport off
    qt off
    
    virkonyV H 2 Replies Last reply
    1
    • M Mason1024

      Hello everyone.
      I tried to use QWebEngineView with Qt 5.7.0 on Windows, I found it dose not work to invoke c++ function from javascript

          QWebChannel *channel{new QWebChannel(this)};
          channel->registerObject(QStringLiteral("VisJsApi"), jsApi);
          channel->registerObject(QStringLiteral("VPntr"), pjso);
      
          webView->page()->setWebChannel(channel);
      
      document.addEventListener("DOMContentLoaded", function() {
          new QWebChannel(qt.webChannelTransport, function(channel) {
              window.VisJsApi = channel.objects.VisJsApi;
              window.VPntr = channel.objects.VPntr;
          });
      });
      

      when I use textQtWebchannel to test.

      function textQtWebchannel() {
      if (typeof QWebChannel != 'undefined') {
              alert("QWebChannel on");
          } else {
              alert("QWebChannel off");
          }
      
          if (typeof navigator != 'undefined') {
              alert("navigator on");
      
              if (typeof navigator.qtWebChannelTransport != 'undefined') {
                  alert("navigator.qtWebChannelTransport on");
              } else {
                  alert("navigator.qtWebChannelTransport off");
              }
          } else {
              alert("navigator off");
          }
      
          if (typeof qt != 'undefined') {
              alert("qt on");
      
              if (typeof qt.webChannelTransport != 'undefined') {
                  alert("qt.webChannelTransport on");
      
                  new QWebChannel(qt.webChannelTransport, function(channel) {
                      window.VisJsApi = channel.objects.VisJsApi;
                      window.VPntr = channel.objects.VPntr;
                  });
      
              } else {
                  alert("qt.webChannelTransport off");
              }
          } else {
              alert("qt off");
          }
      };
      

      what i finially find it print out:

      QWebChannel on
      navigator on
      navigator.qtWebChannelTransport off
      qt off
      
      virkonyV Offline
      virkonyV Offline
      virkony
      wrote on last edited by
      #2

      @Mason1024 I have a bit different problem in QML undef Linux. It works but too fragile.

      ApplicationWindow {
          visible: true
          title: "test: " + web.title
          menuBar: MenuBar {
              Menu {
                  title: qsTr("&File")
                  MenuItem {
                      text: qsTr("&Web")
                      onTriggered: {
                          web.url = "http://meta.ua"
                      }
                  }
                  MenuItem {
                      text: qsTr("&Load")
                      onTriggered: {
                          web.loadHtml('dummy')
                      }
                  }
                  MenuItem {
                      text: qsTr("E&xit")
                      onTriggered: Qt.quit();
                  }
              }
          }
      
          WebEngineView {
              id: web
              webChannel.registeredObjects: []
              anchors.fill: parent
              userScripts: [testScript]
              WebEngineScript {
                  id: testScript
                  sourceCode: "document.title = 'qt = ' + typeof qt"
                  injectionPoint: WebEngineScript.Deferred
                  worldId:  WebEngineScript.MainWorld
              }
          }
      }
      

      More precisely. First time my page is loaded qt.webChannelTransport is visible. But when I switch url or do loadHtml I cannot access qt.webChannelTransport after page loads.
      Besides that when I try to connect with remote web debugging (using env QTWEBENGINE_REMOTE_DEBUGGING=23654) before page loaded I get the same problem on my first page.
      Also qt is inaccessible without setting webChannel.registeredObjects or webChannel itself during component load.
      Looks like it is simply unstable yet that results in strange internal state.

      I'd trye to set url for webView after setting webChannel.

      P.S. Mine qtwebengine is built without system ffmpeg.

      1 Reply Last reply
      0
      • M Mason1024

        Hello everyone.
        I tried to use QWebEngineView with Qt 5.7.0 on Windows, I found it dose not work to invoke c++ function from javascript

            QWebChannel *channel{new QWebChannel(this)};
            channel->registerObject(QStringLiteral("VisJsApi"), jsApi);
            channel->registerObject(QStringLiteral("VPntr"), pjso);
        
            webView->page()->setWebChannel(channel);
        
        document.addEventListener("DOMContentLoaded", function() {
            new QWebChannel(qt.webChannelTransport, function(channel) {
                window.VisJsApi = channel.objects.VisJsApi;
                window.VPntr = channel.objects.VPntr;
            });
        });
        

        when I use textQtWebchannel to test.

        function textQtWebchannel() {
        if (typeof QWebChannel != 'undefined') {
                alert("QWebChannel on");
            } else {
                alert("QWebChannel off");
            }
        
            if (typeof navigator != 'undefined') {
                alert("navigator on");
        
                if (typeof navigator.qtWebChannelTransport != 'undefined') {
                    alert("navigator.qtWebChannelTransport on");
                } else {
                    alert("navigator.qtWebChannelTransport off");
                }
            } else {
                alert("navigator off");
            }
        
            if (typeof qt != 'undefined') {
                alert("qt on");
        
                if (typeof qt.webChannelTransport != 'undefined') {
                    alert("qt.webChannelTransport on");
        
                    new QWebChannel(qt.webChannelTransport, function(channel) {
                        window.VisJsApi = channel.objects.VisJsApi;
                        window.VPntr = channel.objects.VPntr;
                    });
        
                } else {
                    alert("qt.webChannelTransport off");
                }
            } else {
                alert("qt off");
            }
        };
        

        what i finially find it print out:

        QWebChannel on
        navigator on
        navigator.qtWebChannelTransport off
        qt off
        
        H Offline
        H Offline
        huhai1002
        wrote on last edited by
        #3

        @Mason1024 i want to know, too

        1 Reply Last reply
        0
        • M Offline
          M Offline
          maximus
          wrote on last edited by maximus
          #4

          Let me know if you figured a way to make it work, currently stuck on this too.

          I must say the documentation is lacking on QWebEngine.
          The example is too complex for nothing, we don't want a chat server, just a simple WebPage communicating to C++ in both way (printing text and alert message example would be sufficient to understand).
          I don't understand why they use a QWebSocket since it's not necessary?
          Any more example around that could help? I did everything the docs says and I can't get it to work js-->c++, the other way is easy with runJavascript


          Free Indoor Cycling Software - https://maximumtrainer.com

          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