Qt Forum

    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Unsolved

    Unsolved Cpp to JS: function with struct as parameter

    QtWebEngine
    3
    11
    1430
    Loading More Posts
    • 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.
    • S
      Slei last edited by

      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?

      JonB 1 Reply Last reply Reply Quote 0
      • JonB
        JonB @Slei last edited by

        @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?

        1 Reply Last reply Reply Quote 0
        • S
          Slei last edited by

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

          1 Reply Last reply Reply Quote 0
          • S
            Slei last edited by

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

            1 Reply Last reply Reply Quote 0
            • kkoehne
              kkoehne Moderators last edited by kkoehne

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

              Director R&D, The Qt Company

              1 Reply Last reply Reply Quote 1
              • S
                Slei last edited by

                @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 1 Reply Last reply Reply Quote 0
                • kkoehne
                  kkoehne Moderators @Slei last edited by

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

                  Director R&D, The Qt Company

                  1 Reply Last reply Reply Quote 3
                  • S
                    Slei last edited by

                    // 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

                    1 Reply Last reply Reply Quote 0
                    • S
                      Slei last edited by

                      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

                      1 Reply Last reply Reply Quote 0
                      • S
                        Slei last edited by

                        but i've also found the following issue:

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

                        1 Reply Last reply Reply Quote 0
                        • S
                          Slei last edited by

                          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

                          1 Reply Last reply Reply Quote 1
                          • First post
                            Last post