Cpp to JS: function with struct as parameter
-
Hey,
I'm currentlry trying to emit an js function on cpp
js: testTextMessage(msg: TextMessage)
c++:
.h
struct TextMessage { int sentTime; QString messageText; }; Q_DECLARE_METATYPE(TextMessage) Q_INVOKABLE void testTextMessage(TextMessage msg);
.cpp
in an Widget Ctor where the QWebEngine is created:qRegisterMetaType<TextMessage>("TextMessage");
Call:
TextMessage msg; msg.messageText = "Frohen Joghurt mit Bier"; msg.sentTime = 13374; emit testTextMessage(msg);
But i get following error when emitting:
"Uncaught TypeError: Cannot read property 'messageText' of null", source: qrc:///web/index.html (929)
can anyone help?
-
Hey,
I'm currentlry trying to emit an js function on cpp
js: testTextMessage(msg: TextMessage)
c++:
.h
struct TextMessage { int sentTime; QString messageText; }; Q_DECLARE_METATYPE(TextMessage) Q_INVOKABLE void testTextMessage(TextMessage msg);
.cpp
in an Widget Ctor where the QWebEngine is created:qRegisterMetaType<TextMessage>("TextMessage");
Call:
TextMessage msg; msg.messageText = "Frohen Joghurt mit Bier"; msg.sentTime = 13374; emit testTextMessage(msg);
But i get following error when emitting:
"Uncaught TypeError: Cannot read property 'messageText' of null", source: qrc:///web/index.html (929)
can anyone help?
-
Where do you execute your JS? As part of a QWebEnginePage , as the category 'QtWebEngine' hints at?
If so, the direct export of types with qRegisterMetaType doesn't work. Web pages are rendered in a separate process, isolated from the rest of your program. You need something like QWebChannel to communicate back and forth ...
-
@kkoehne said in Cpp to JS: function with struct as parameter:
You need something like QWebChannel to communicate back and forth ...
I think I already work with QWebEngine. Communitcating with simple parameters works fine(Q Types and built in types)
But I'm not fully sure how I can support user-defined types. -
@kkoehne said in Cpp to JS: function with struct as parameter:
You need something like QWebChannel to communicate back and forth ...
I think I already work with QWebEngine. Communitcating with simple parameters works fine(Q Types and built in types)
But I'm not fully sure how I can support user-defined types.@Slei , can you post some complete example code?
THere's no direct way to expose a C++ type to JS inside a QWebEnginePage - you'd need to recreate the type yourself using JS.
If you however want to expose your type in QML/JS, see http://doc.qt.io/qt-5/qtqml-cppintegration-definetypes.html for a full overview of the various types.
-
// the webengine view widget
m_wev = new QWebEngineView(this);
QWebChannel* channel = new QWebChannel(m_wev);channel->registerObject(QString("ChatWidget"), this); m_wev->page()->setWebChannel(channel); m_wev->load(QUrl("qrc:///web/index.html")); m_wev->show();
is the way I'm creating the webcontent widget
and the rest is as written in my initial post.
I just want to send a struct to the Js from c++.
It does work with a QList<QString>, QString etc.I think the other obvious solution might be using json and creating it myself, but i just wanna know if Qt might serialize it itself when I register my struct/class correctly
-
Here they suggest to use qRegisterMetaType:
Which seems not to work for me
-
but i've also found the following issue: