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. Alternative of addToJavaScriptWindowObject in Qt 5.12.4
Forum Updated to NodeBB v4.3 + New Features

Alternative of addToJavaScriptWindowObject in Qt 5.12.4

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

    I am trying to migrate my code from QWebView to QWebEngine. I want to add an object from my code into the javascript. In QWebView it was possible with the function addToJavaScriptWindowObject. How can we do this in QWebEngine.

    webview->page()->mainFrame()->addToJavaScriptWindowObject("qtObject", this);

    Our java script need to call functions of "qtObject".

    Gojir4G 1 Reply Last reply
    0
    • H Hetal

      I am trying to migrate my code from QWebView to QWebEngine. I want to add an object from my code into the javascript. In QWebView it was possible with the function addToJavaScriptWindowObject. How can we do this in QWebEngine.

      webview->page()->mainFrame()->addToJavaScriptWindowObject("qtObject", this);

      Our java script need to call functions of "qtObject".

      Gojir4G Offline
      Gojir4G Offline
      Gojir4
      wrote on last edited by
      #2

      @Hetal Hi,

      According to this post on stackoverflow, It seems you need to use QWebChannel with QWebView.

      Here is some link with more informations:
      Qt WebChannel – bridging the gap between C++/QML and the web
      Interacting with HTML from QML over WebChannel/WebSockets
      QTDD14 - QtWebChannel - Bridging the Gap between HTML and Qt - Milian Wolff, KDAB

      H 1 Reply Last reply
      1
      • Gojir4G Gojir4

        @Hetal Hi,

        According to this post on stackoverflow, It seems you need to use QWebChannel with QWebView.

        Here is some link with more informations:
        Qt WebChannel – bridging the gap between C++/QML and the web
        Interacting with HTML from QML over WebChannel/WebSockets
        QTDD14 - QtWebChannel - Bridging the Gap between HTML and Qt - Milian Wolff, KDAB

        H Offline
        H Offline
        Hetal
        wrote on last edited by
        #3

        @Gojir4 Thanks a lot, Our Qt application is already using QWebSocket, can we use the same QWebSocket for establishing the Web Channel, as per our understanding QWebSocket is heavy object.
        We have referred the "Qt WebChannel Standalone Example". Also, please note that we do not require to send messages to server using QWebChannel. We only want, method of "qtObject"' gets called from server's java script code
        .
        So the question is whether QWebSocketServer is required to have QWebChannel to work?
        or Only QWebChannel and QWebChannelAbstractTransport are required to be implemented.

        Please suggest.

        M 1 Reply Last reply
        0
        • H Hetal

          @Gojir4 Thanks a lot, Our Qt application is already using QWebSocket, can we use the same QWebSocket for establishing the Web Channel, as per our understanding QWebSocket is heavy object.
          We have referred the "Qt WebChannel Standalone Example". Also, please note that we do not require to send messages to server using QWebChannel. We only want, method of "qtObject"' gets called from server's java script code
          .
          So the question is whether QWebSocketServer is required to have QWebChannel to work?
          or Only QWebChannel and QWebChannelAbstractTransport are required to be implemented.

          Please suggest.

          M Offline
          M Offline
          MAZHE 0
          wrote on last edited by
          #4

          @Hetal Hi, you may find the previous post below useful to your problem:

          https://forum.qt.io/topic/70968/qwebchannel-js-uncaught-referenceerror-qt-is-not-defined

          This previous post assumed that you have no control over the js code that will run in your QWebEngine.

          If you do have the control over the js code, you may simply register your C++ obj using

          //your C++ code here
          QWebChannel* pWebChannel = new QWebChannel(this);
          pWebChannel ->registerObject(nameOfC++Obj, pC++Obj);
          
          QWebEnginePage *pWebPage = new QWebEnginePage(pWebProfile);
          pWebPage->setWebChannel(pWebChannel );
          
          
          

          add the following part to your js code:

          //your js code here
           <script type="text/javascript" src="qrc:///qtwebchannel/qwebchannel.js"></script>
          
           if (typeof qt != 'undefined') {
                          new QWebChannel(qt.webChannelTransport, function(channel) {
                                  if (typeof channel.objects != 'undefined') {
                                          YouJsObj = channel.objects.YourC++Obj;
          }
          });
          }
          
          
          
          
          1 Reply Last reply
          1

          • Login

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