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

qtwebview runJavaScript callback returns always string. If not returns empty string.

    XMLWordPrintable

Details

    • Bug
    • Resolution: Unresolved
    • P2: Important
    • None
    • 5.5.0 Beta
    • WebView
    • None

    Description

      Hey,

      I 'm developing a webview/webengine based cross platform application. But it has problems to passing result data. I know that WebEngine & WebView totally different modules, but, at least some behaviours should match with from to another like running javascript & getting results.

      import QtQuick 2.4
      import QtQuick.Controls 1.3
      import QtWebView 1.1
      import App 1.0
      
      ApplicationWindow {
          title: qsTr("Hello World")
          width: 640
          height: 480
          visible: true
      
          WebView {
              id: webView
      
              anchors.fill: parent
      
              onLoadingChanged: console.log('LOADING CHANGED:', loading)
              onLoadProgressChanged: console.log('LOAD PROGRESS:', loadProgress);
      
              Component.onCompleted: loadHtml(util.fileContent(":/web.html"))
          }
      
          Util {
              id: util
          }
      
          Timer {
              running: true
              interval: 2000
              repeat: false
              triggeredOnStart: false
              onTriggered: {
                  webView.runJavaScript('getObjectData();', function(result) {
                      console.log('getObjectData() -> ', typeof result, result, JSON.stringify(result));
                  });
      
                  webView.runJavaScript('getStringData();', function(result) {
                      console.log('getStringData() -> ', typeof result, result, JSON.stringify(result));
                  });
      
                  webView.runJavaScript('getIntData();', function(result) {
                      console.log('getIntData() -> ', typeof result, result, JSON.stringify(result));
                  });
      
                  webView.runJavaScript('getBoolData();', function(result) {
                      console.log('getBoolData() -> ', typeof result, result, JSON.stringify(result));
                  });
      
                  webView.runJavaScript('getObjectDataInterface();', function(result) {
                      result = JSON.parse(result);
                      console.log('getObjectDatInterfacea() -> ', typeof result, result, JSON.stringify(result));
                  });
      
                  webView.runJavaScript('getStringDataInterface();', function(result) {
                      result = JSON.parse(result);
                      console.log('getStringDataInterface() -> ', typeof result, result, JSON.stringify(result));
                  });
      
                  webView.runJavaScript('getIntDataInterface();', function(result) {
                      result = JSON.parse(result);
                      console.log('getIntDataInterface() -> ', typeof result, result, JSON.stringify(result));
                  });
      
                  webView.runJavaScript('getBoolDataInterface();', function(result) {
                      result = JSON.parse(result);
                      console.log('getBoolDataInterface() -> ', typeof result, result, JSON.stringify(result));
                  });
              }
          }
      }
      

      web.html

      <!DOCTYPE html>
      <html>
      <head>
      <script type="text/javascript">
      function getObjectData() {
          return {
              someAwesomeKey: "awesomeData"
          };
      }
      
      function getStringData() {
          return "I 'm a string!";
      }
      
      function getIntData() {
          return 10;
      }
      
      function getBoolData() {
          return true;
      }
      
      function returnData(data) {
          return JSON.stringify(data);
      }
      
      function getObjectDataInterface() {
          return returnData(getObjectData());
      }
      
      function getStringDataInterface() {
          return returnData(getStringData());
      }
      
      function getIntDataInterface() {
          return returnData(getIntData());
      }
      
      function getBoolDataInterface() {
          return returnData(getBoolData());
      }
      
      </script>
      </head>
      <body>
      <h1>Just a dummy interface</h1>
      </body>
      </html>
      
      

      The output is:

      qml: LOAD PROGRESS: 100
      qml: getObjectData() ->  string  ""
      qml: getStringData() ->  string I 'm a string! "I 'm a string!"
      qml: getIntData() ->  string 10 "10"
      qml: getBoolData() ->  string true "true"
      qml: getObjectDatInterfacea() ->  object [object Object] {"someAwesomeKey":"awesomeData"}
      qml: getStringDataInterface() ->  string I 'm a string! "I 'm a string!"
      qml: getIntDataInterface() ->  number 10 10
      qml: getBoolDataInterface() ->  boolean true true
      

      As you can see, we can workaround by using json for data conversation. May be there is a cost for doing that. But for webengine compatibility, I think this can be use internally.

      Attachments

        1. webviewimpr.zip
          3 kB
          Firat Agdas
        No reviews matched the request. Check your Options in the drop-down menu of this sections header.

        Activity

          People

            stromme Christian
            firatagdas Firat Agdas
            Votes:
            1 Vote for this issue
            Watchers:
            1 Start watching this issue

            Dates

              Created:
              Updated:

              Gerrit Reviews

                There are no open Gerrit changes