Cpp to JS: function with struct as parameter

  • Hey,

    I'm currentlry trying to emit an js function on cpp

    js: testTextMessage(msg: TextMessage)



    struct TextMessage
      int sentTime;
      QString messageText;
     Q_INVOKABLE void testTextMessage(TextMessage msg);

    in an Widget Ctor where the QWebEngine is created:



    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?

  • @Slei
    Not my area quite so excuse if dumb question, but are we supposed to see the implementation code for your testTextMessage(TextMessage msg), because somewhere your msg is null?

  • the implementation of this function is JS side and it only creates and console output with the struct data

  • I still haven't really found any solution to this.
    Has anyone any idea what might be the issue here?

  • Moderators

    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.

  • Moderators

    @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);

    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:


  • Ok my solution to this:

    I manually create a QJsonObject for my custom type and send it as parameter, on the js everything stayed the same since js already deserializes the data

Log in to reply

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.