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. QJsonDocument as parameter in emit : when i read the json in javascpit it returns null
Forum Updated to NodeBB v4.3 + New Features

QJsonDocument as parameter in emit : when i read the json in javascpit it returns null

Scheduled Pinned Locked Moved Solved QtWebEngine
5 Posts 2 Posters 714 Views 1 Watching
  • 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.
  • elicatE Offline
    elicatE Offline
    elicat
    wrote on last edited by
    #1

    HI,
    I have a project with qtwebengine where I call a method that prepares a json in QJsonDocument format and then puts it as a parameter in an emit signal.
    When I read the json in the function that receives the data in javascript the json is empty.
    where am i wrong?
    Can i create a json and receive a json in the html/javascript page without going through the conversion to string?

    Thanks in advance

    Code with return null or {} with parameter type QJsonObject

    	json.setArray(recordsArray);
    	QJsonObject json_obj = json.object();
    	emit testJsonReturn(json_obj);
    

    OR
    Code with return null or {} with parameter type QJsonDocument

    	json.setArray(recordsArray);
    	emit testJsonReturn(json);
    

    If I convert the same data into string and pass it to javascript the data is present.
    QJsonDocument as parameter in emit : when i read the json in javascpit it returns null
    Code correct:

    	json.setArray(recordsArray);
    	strJsonDati = json.toJson(QJsonDocument::Compact);
    	emit testJsonReturn(strJsonDati);
    

    Saluti, Gianfranco Elicat

    JonBJ 1 Reply Last reply
    0
    • elicatE elicat

      HI,
      I have a project with qtwebengine where I call a method that prepares a json in QJsonDocument format and then puts it as a parameter in an emit signal.
      When I read the json in the function that receives the data in javascript the json is empty.
      where am i wrong?
      Can i create a json and receive a json in the html/javascript page without going through the conversion to string?

      Thanks in advance

      Code with return null or {} with parameter type QJsonObject

      	json.setArray(recordsArray);
      	QJsonObject json_obj = json.object();
      	emit testJsonReturn(json_obj);
      

      OR
      Code with return null or {} with parameter type QJsonDocument

      	json.setArray(recordsArray);
      	emit testJsonReturn(json);
      

      If I convert the same data into string and pass it to javascript the data is present.
      QJsonDocument as parameter in emit : when i read the json in javascpit it returns null
      Code correct:

      	json.setArray(recordsArray);
      	strJsonDati = json.toJson(QJsonDocument::Compact);
      	emit testJsonReturn(strJsonDati);
      
      JonBJ Offline
      JonBJ Offline
      JonB
      wrote on last edited by JonB
      #2

      @elicat
      You don't show how you try to pass and receive the QJson... objects from C++ to JavaScript in the QtWebEngine page. By the way, the fact your code uses emit is not relevant.

      But whatever. The C++ QJson... classes do not have the same representation as the JavaScript JSON objects. You cannot swap between these. So converting to string at sender side and re-parsing from string at receiver side seems reasonable/the only approach.

      EDIT
      Hmm, perhaps I spoke too soon. I had a look at QJSEngine Class --- is that what you use with QtWebEngine? [I thought it was QML, dunno.] If it is that has template <typename T> QJSValue QJSEngine::toScriptValue(const T &value). https://stackoverflow.com/a/75154459/489865 claims you can pass a QJsonObject or QJsonArray but not a QJsonDocument.

      I am not understanding what you have in code which connects your testJsonReturn() C++ signal to something in JavaScript on a QWebEngine page?

      elicatE 1 Reply Last reply
      0
      • JonBJ JonB

        @elicat
        You don't show how you try to pass and receive the QJson... objects from C++ to JavaScript in the QtWebEngine page. By the way, the fact your code uses emit is not relevant.

        But whatever. The C++ QJson... classes do not have the same representation as the JavaScript JSON objects. You cannot swap between these. So converting to string at sender side and re-parsing from string at receiver side seems reasonable/the only approach.

        EDIT
        Hmm, perhaps I spoke too soon. I had a look at QJSEngine Class --- is that what you use with QtWebEngine? [I thought it was QML, dunno.] If it is that has template <typename T> QJSValue QJSEngine::toScriptValue(const T &value). https://stackoverflow.com/a/75154459/489865 claims you can pass a QJsonObject or QJsonArray but not a QJsonDocument.

        I am not understanding what you have in code which connects your testJsonReturn() C++ signal to something in JavaScript on a QWebEngine page?

        elicatE Offline
        elicatE Offline
        elicat
        wrote on last edited by
        #3

        @JonB hello,
        yes the connect is from qtwebengine and I have try also with QJsonObject (see previous example .

        Tihis is code for connect webengine

        window.onload = function () {
            new QWebChannel(qt.webChannelTransport, function (channel) {
                webobj = channel.objects.wiseMan;
                window.foo = webobj;
                // connect signal whith js code
                webobj.testJsonReturn.connect(testJsonReturn);
        
                var jsonData = {}
               jsonData.xxx = "xx" // example
              jsonData.boxWidth = withContentBox.toString();
              foo.sqlToJson(jsonData); // call C++
            });
        }
        

        This is example js code

        function testJsonReturn(JsonDataReturn) {
            console.log(JSON.stringify(JsonDataReturn));
        }
        
        

        and I have result

        {}
        

        Saluti, Gianfranco Elicat

        JonBJ 1 Reply Last reply
        0
        • elicatE elicat

          @JonB hello,
          yes the connect is from qtwebengine and I have try also with QJsonObject (see previous example .

          Tihis is code for connect webengine

          window.onload = function () {
              new QWebChannel(qt.webChannelTransport, function (channel) {
                  webobj = channel.objects.wiseMan;
                  window.foo = webobj;
                  // connect signal whith js code
                  webobj.testJsonReturn.connect(testJsonReturn);
          
                  var jsonData = {}
                 jsonData.xxx = "xx" // example
                jsonData.boxWidth = withContentBox.toString();
                foo.sqlToJson(jsonData); // call C++
              });
          }
          

          This is example js code

          function testJsonReturn(JsonDataReturn) {
              console.log(JSON.stringify(JsonDataReturn));
          }
          
          

          and I have result

          {}
          
          JonBJ Offline
          JonBJ Offline
          JonB
          wrote on last edited by JonB
          #4

          @elicat
          You did not mention that you are using QWebChannel, do you not think that was relevant?

          I have never looked at it. I believe it allows the connection of signals from the C++ side to slots on the JS side.

          However, you are passing a C++/Qt QJson... object as a parameter from C++ to JS. And then you are trying to use the JS JSON class/calls on that. Unless you/someone says they are in any way compatible, my guess is that these two internal representation of JSON objects have nothing in common? In which case you won't be able to have them interact like this. At best you might use the webobj object to call C++-side QJson... methods on the JSON objects instead of the JS-side JSON calls.

          Your question was asked in How send a QJsonObject using QWebChannel in Qt. The answer there was

          Instead of using the QJson family objects, you can send QVariant objects to your Javascript code

          elicatE 1 Reply Last reply
          0
          • JonBJ JonB

            @elicat
            You did not mention that you are using QWebChannel, do you not think that was relevant?

            I have never looked at it. I believe it allows the connection of signals from the C++ side to slots on the JS side.

            However, you are passing a C++/Qt QJson... object as a parameter from C++ to JS. And then you are trying to use the JS JSON class/calls on that. Unless you/someone says they are in any way compatible, my guess is that these two internal representation of JSON objects have nothing in common? In which case you won't be able to have them interact like this. At best you might use the webobj object to call C++-side QJson... methods on the JSON objects instead of the JS-side JSON calls.

            Your question was asked in How send a QJsonObject using QWebChannel in Qt. The answer there was

            Instead of using the QJson family objects, you can send QVariant objects to your Javascript code

            elicatE Offline
            elicatE Offline
            elicat
            wrote on last edited by
            #5

            @JonB Hello thanks for your precision.

            Is there another way to make a c++ method communicate with a javascript code of an html page loaded in a qml file? If there is I didn't know about it.
            I have inserted the post in the QtWebEngine section specially.
            Anyway, It is true that questions asked in the right way are easier to interpret and solve.

            After seeing the proposed link I solved it as follows.

            	QJsonDocument json(recordsArray);
            	json.setArray(recordsArray);
            	QJSEngine engine;
            	QJSValue resDatiSqlJsonValue;
            	if (json.isObject())
            	{
            		resDatiSqlJsonValue = engine.toScriptValue(json.object());
            	}
            	else if (json.isArray())
            	{
            		resDatiSqlJsonValue = engine.toScriptValue(json.array());
            	}
            	else {
            		resDatiSqlJsonValue = engine.newErrorObject(
            			QJSValue::TypeError,
            			"JSON was neither an object nor array.  Strange, right?"
            		);
            	}
            	emit testJsonReturn(resDatiSqlJsonValue);
            

            Thanks

            Saluti, Gianfranco Elicat

            1 Reply Last reply
            0
            • elicatE elicat has marked this topic as solved on

            • Login

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