Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. QtWebEngine
  4. Cpp to JS: function with struct as parameter

Cpp to JS: function with struct as parameter

Scheduled Pinned Locked Moved Unsolved QtWebEngine
11 Posts 3 Posters 2.2k Views
  • 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 Offline
    S Offline
    Slei
    wrote on last edited by
    #1

    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?

    JonBJ 1 Reply Last reply
    0
    • S Slei

      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?

      JonBJ Online
      JonBJ Online
      JonB
      wrote on last edited by
      #2

      @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
      0
      • S Offline
        S Offline
        Slei
        wrote on last edited by
        #3

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

        1 Reply Last reply
        0
        • S Offline
          S Offline
          Slei
          wrote on last edited by
          #4

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

          1 Reply Last reply
          0
          • kkoehneK Offline
            kkoehneK Offline
            kkoehne
            Moderators
            wrote on last edited by kkoehne
            #5

            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
            1
            • S Offline
              S Offline
              Slei
              wrote on last edited by
              #6

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

              kkoehneK 1 Reply Last reply
              0
              • S Slei

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

                kkoehneK Offline
                kkoehneK Offline
                kkoehne
                Moderators
                wrote on last edited by
                #7

                @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
                3
                • S Offline
                  S Offline
                  Slei
                  wrote on last edited by
                  #8

                  // 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
                  0
                  • S Offline
                    S Offline
                    Slei
                    wrote on last edited by
                    #9

                    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
                    0
                    • S Offline
                      S Offline
                      Slei
                      wrote on last edited by
                      #10

                      but i've also found the following issue:

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

                      1 Reply Last reply
                      0
                      • S Offline
                        S Offline
                        Slei
                        wrote on last edited by
                        #11

                        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
                        1

                        • Login

                        • Login or register to search.
                        • First post
                          Last post
                        0
                        • Categories
                        • Recent
                        • Tags
                        • Popular
                        • Users
                        • Groups
                        • Search
                        • Get Qt Extensions
                        • Unsolved