title: QT QWebchannel not able to access CPP object methods or properties
body: <p>I'm using the QT 5.9 WebEngine framework to display web pages. I'm injecting javascript into a page when it loads, and want to allow the javascript to be able to access a QT object. </p>
  
  <p>I get the QWebchannel callback in JS to be invoked but the object method and properties are undefined.</p>
  
  <p><strong>NOTE</strong>: I NEED to inject all JS code and cannot change the html code of the loaded page. Meaning i cannot include any js script nor write any js code directly in the html pages.</p>
  
  <p>Here is my code:</p>
  
  <p>I create the QWebEnginePage object with a profile and inject the JS files as below:</p>
  
  <pre><code>// file: webview.cpp, webview extends **QWebEngineView**
  
  QWebEngineScript script;
  script.setSourceCode(qwebchannelsource); // source is qwebchannel.js
  script.setInjectionPoint(QWebEngineScript::DocumentCreation);
  profile-&gt;scripts()-&gt;insert(script);
  QWebEnginePage page* = new QWebEnginePage(profile, this);
  
  QWebChannel *channel = new QWebChannel(page);
  page-&gt;setWebChannel(channel);
  channel-&gt;registerObject(QStringLiteral("jshelper"), &amp;JSHelper::instance());
  </code></pre>
  
  <p><strong>JSHelper</strong></p>
  
  <pre><code>class JSHelper : public QObject
  {
  public:
      static JSHelper &amp;instance();
      QString someString;
      int someInt;
  
      Q_INVOKABLE int getInt();
      Q_PROPERTY(int myIntInCppSide READ getInt)
  
  private:
      JSHelper();
  };
  </code></pre>
  
  <p>JS code</p>
  
  <pre><code>// also injected into the page (like qwebchannel.js) at QWebEngineScript::DocumentReady
  
  new QWebChannel(qt.webChannelTransport, function (channel) {
      var jshelper = channel.objects.jshelper;
      console.warn('qwebchannel triggered');
  
      // do what you gotta do
      if (jshelper === undefined) {
          console.warn("jshelper is undefined");
      }
      else {
          console.warn("jshelper is awesome");
      }
  
      console.warn("jshelper value of string " + jshelper.somestring);
      console.warn("jshelper value of int " + jshelper.myIntInCppSide);
      jshelper.myIntInCppSide(function (n) {
          console.warn("jshelper value of int " + n);
      });
  });
  </code></pre>
  
  <p>my output:</p>
  
  <pre><code>qwebchannel triggered"
  jshelper is awesome"
  jshelper value of string undefined"
  jshelper value of int undefined"
  Uncaught TypeError: jshelper.myIntInCppSide is not a function"
  </code></pre>
  
  <p>As you can see I have tried various suggestions from several answers but nothing seems to fix my issue that the function does not exist. 
  Even when using the remote debugging I can see the jshelper is of type QObject but it does not have the properties nor methods of my class.</p>
  
  <p><a href="https://stackoverflow.com/questions/31928444/qt-qwebenginepagesetwebchannel-transport-object">QT QWebEnginePage::setWebChannel() transport object</a></p>
  
  <p><a href="https://stackoverflow.com/questions/33704128/undefined-properties-and-return-types-when-using-qwebchannel">Undefined properties and return types when using QWebChannel</a></p>