Uploaded image for project: 'Qt'
  1. Qt
  2. QTBUG-48198

[QWebChannel] Cannot use simple JavaScript objects as parameters to methods invoked via a QWebChannel

    XMLWordPrintable

Details

    • Bug
    • Resolution: Unresolved
    • P2: Important
    • None
    • 5.5.0
    • WebChannel
    • None

    Description

      I have the following code in an HTML file (loaded into a QWebEnginePage):

      function runTest() {
              var _ = new QWebChannel(qt.webChannelTransport, function(channel) {
                      var obj = { "testObject": "yes" };
                      channel.objects.proxy.logVariant(obj);
                      channel.objects.proxy.logJSONValue(obj);
                      channel.objects.proxy.logJSONObject(obj);
                      channel.objects.proxy.logString(JSON.stringify(obj));
              });
              document.write("Loaded!");
      }
      

      On the C++ side the methods are defined as:

      Q_INVOKABLE void logVariant(const QVariant &val);
      Q_INVOKABLE void logJSONValue(const QJsonValue &val);
      Q_INVOKABLE void logJSONObject(const QJsonObject &val);
      Q_INVOKABLE void logString(const QString &val);
      
      void WebProxy::logVariant(const QVariant &val)
      {
      	qDebug() << "VARIANT VAL:" << val.toJsonValue();
      	qDebug() << "VARIANT OBJ:" << val.toJsonObject();
      }
      
      void WebProxy::logJSONValue(const QJsonValue &val)
      {
      	qDebug() << "JSON VALUE:" << val;
      }
      
      void WebProxy::logJSONObject(const QJsonObject &val)
      {
      	qDebug() << "JSON OBJECT:" << val;
      }
      
      void WebProxy::logString(const QString &val)
      {
      	qDebug() << "STRING:" << val;
      }
      

      (you can find the full compilable project here: https://github.com/ileonte/qwebchantest)

      When ran the program outputs the following:

      VARIANT VAL: QJsonValue(null)
      VARIANT OBJ: QJsonObject()
      Could not convert argument QJsonValue(object, QJsonObject({"testObject":"yes"})) to target type "QJsonValue" .
      Could not convert argument QJsonValue(object, QJsonObject({"testObject":"yes"})) to target type "QJsonObject" .
      STRING: "{\"testObject\":\"yes\"}"
      

      I've confirmed this behavior on both Linux (gcc) and Windows (MSVC2013) both 64bit.

      Attachments

        For Gerrit Dashboard: QTBUG-48198
        # Subject Branch Project Status CR V

        Activity

          People

            milianw Milian Wolff
            ileonte IonuČ› Leonte
            Votes:
            5 Vote for this issue
            Watchers:
            10 Start watching this issue

            Dates

              Created:
              Updated:

              Gerrit Reviews

                There are no open Gerrit changes