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. Pass custome data from C++ to JS (QWebChannel)
Forum Updated to NodeBB v4.3 + New Features

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

Scheduled Pinned Locked Moved QtWebEngine
qwebchannelqwebengineviewcustome datablinkchromium
1 Posts 1 Posters 2.1k 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.
  • C Offline
    C Offline
    cr0_
    wrote on 13 Oct 2015, 09:15 last edited by
    #1

    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
    1

    1/1

    13 Oct 2015, 09:15

    • Login

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