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?



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

    https://stackoverflow.com/questions/34818557/how-to-register-a-class-for-use-it-in-a-qwebchannel-signal-in-qt

    Which seems not to work for me



  • but i've also found the following issue:

    https://bugreports.qt.io/browse/QTBUG-61515



  • 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.