Qt Forum

    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Unsolved

    Update: Forum Guidelines & Code of Conduct


    Qt World Summit: Early-Bird Tickets

    Pass custome data from C++ to JS (QWebChannel)

    QtWebEngine
    qwebchannel qwebengineview custome data blink chromium
    1
    1
    1751
    Loading More Posts
    • 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.
    • C
      cr0_ last edited by

      Hello, I use QWebEngineView for render my HTML page and QWebChannel for communicate with my JS code. I can pass QVariant type into JS code as signal parameter or function return value. But I need to pass custome class object. I wrote the class and inherit it from QObject like this:
      <pre><code>
      #include <QObject>

      class test : public QObject
      {
      Q_OBJECT

      public:
      test() : QObject(nullptr) {}
      test(const test&) : QObject(nullptr) {}
      test(QObject *parent) : QObject(nullptr) {};

      int a;
      int b;
      

      };

      Q_DECLARE_METATYPE(test);
      </pre></code>

      than I registered type as:
      qRegisterMetaType<test>();

      and emit signal:

      test t;
      t.a = 666;
      t.b = 454;
      emit home_page_controller.MySignal(t);

      In JS code I connect to signal ant try to show received data:
      <pre><code>
      document.addEventListener("DOMContentLoaded", function ()
      {
      new QWebChannel(qt.webChannelTransport, function (channel)
      {
      window.home_page_controller = channel.objects.home_page_controller;

      	window.home_page_controller.MySignal.connect(function(struct)
      	{
      		alert(struct)
      	})
          });
      

      });
      </pre></code>
      But instead [Object] I recived null.

      Next my step - add my structure into QVariant:

      QVariant var;
      test t;
      t.a = 666;
      t.b = 454;
      var.setValue(t);
      emit home_page_controller.MySignal(var);

      But null received again. How I can to pass custome data into JS code? Thanks.

      1 Reply Last reply Reply Quote 1
      • First post
        Last post