Pass custome data from C++ to JS (QWebChannel)
-
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_OBJECTpublic:
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.