Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. QML and Qt Quick
  4. How to display received HTML from an API to webview ?
Forum Updated to NodeBB v4.3 + New Features

How to display received HTML from an API to webview ?

Scheduled Pinned Locked Moved Solved QML and Qt Quick
qt quickwebviewhtml
8 Posts 3 Posters 3.6k Views 2 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.
  • Q Offline
    Q Offline
    Qjay
    wrote on 23 May 2016, 11:16 last edited by A Former User
    #1

    Hey , everyone i am trying to retrieve a page content from an API . the API returns an JSON object . After parsing it i get my HTML and store it in a variable .
    I am able to show that variable's content (HTML) in text , but i want to show it in webview how am i supposed to do it ?

    P.S. don't mind the commented code , those were just for testing :)

    You can see my code here :

    import QtQuick 2.5
    import QtQuick.Window 2.2
    import QtWebKit 3.0
    
    Window {
        visible: true
        width: 1024
        height: 600
    
        MouseArea {
            anchors.fill: parent
            onClicked: {
                parsing();
    
            }
            function parsing() {
            var http = new XMLHttpRequest();
           var json , parse , text ;
    
             http.onreadystatechange = function(){
               if(http.readyState == 4 && http.status == 200)
               { json = http.responseText;
                // alert(json);
                parse = JSON.parse(json);
               // console.log(parse.parse.text["*"]);
                   text = parse.parse.text["*"];
                   console.log(text);
                   label.text = text
               }
             };
                   http.open('GET','http://en.wikitolearn.org/api.php?action=parse&page=Intro%20cryptography&format=json');
                  http.send();
        }
        }
    
        Text {
            id:label
    
            text:  "click me "
            anchors.centerIn: parent
        }
    
    
       }
    
    R 1 Reply Last reply 23 May 2016, 11:33
    0
    • Q Qjay
      23 May 2016, 11:16

      Hey , everyone i am trying to retrieve a page content from an API . the API returns an JSON object . After parsing it i get my HTML and store it in a variable .
      I am able to show that variable's content (HTML) in text , but i want to show it in webview how am i supposed to do it ?

      P.S. don't mind the commented code , those were just for testing :)

      You can see my code here :

      import QtQuick 2.5
      import QtQuick.Window 2.2
      import QtWebKit 3.0
      
      Window {
          visible: true
          width: 1024
          height: 600
      
          MouseArea {
              anchors.fill: parent
              onClicked: {
                  parsing();
      
              }
              function parsing() {
              var http = new XMLHttpRequest();
             var json , parse , text ;
      
               http.onreadystatechange = function(){
                 if(http.readyState == 4 && http.status == 200)
                 { json = http.responseText;
                  // alert(json);
                  parse = JSON.parse(json);
                 // console.log(parse.parse.text["*"]);
                     text = parse.parse.text["*"];
                     console.log(text);
                     label.text = text
                 }
               };
                     http.open('GET','http://en.wikitolearn.org/api.php?action=parse&page=Intro%20cryptography&format=json');
                    http.send();
          }
          }
      
          Text {
              id:label
      
              text:  "click me "
              anchors.centerIn: parent
          }
      
      
         }
      
      R Offline
      R Offline
      raven-worx
      Moderators
      wrote on 23 May 2016, 11:33 last edited by
      #2

      @Qjay
      have looked at the docs?!

      --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
      If you have a question please use the forum so others can benefit from the solution in the future

      1 Reply Last reply
      0
      • ? Offline
        ? Offline
        A Former User
        wrote on 23 May 2016, 11:33 last edited by A Former User
        #3

        Hi, and welcome to the Qt forum! You can use WebEngineView and its loadHtml() function for this:

        import QtQuick 2.6
        import QtQuick.Window 2.2
        import QtWebEngine 1.2
        
        Window {
            visible: true
            width: 600
            height: 400
        
            WebEngineView {
                id: webEngineView
                anchors.fill: parent
            }
        
            Component.onCompleted: {
                var html = "<html><head><title>nix</title></head><body><h1>Hallo!</h1></body></html>\n"
                webEngineView.loadHtml(html)
            }
        }
        
        1 Reply Last reply
        0
        • Q Offline
          Q Offline
          Qjay
          wrote on 23 May 2016, 13:41 last edited by
          #4

          Hey , thanks for the reply :) but all i am getting is blank screen :(

          P.S. i am sure that parsing() returns string since i have checked it using typeof

          here is the code :

          import QtQuick 2.5
          import QtQuick.Window 2.2
          import QtWebEngine 1.1
          import QtWebKit 3.0
          
          
          Window {
              visible: true
              width: 600
              height: 400
          
              function parsing() {
                      var http = new XMLHttpRequest();
                     var json , parse , text ;
          
                       http.onreadystatechange = function(){
                         if(http.readyState == 4 && http.status == 200)
                         { json = http.responseText;
          
                          parse = JSON.parse(json);
          
                             text = parse.parse.text["*"];
                             console.log(text);
                            return(text);
                         }
                       };
                             http.open('GET','http://en.wikitolearn.org/api.php?action=parse&page=Linear%20Algebra/Sets&format=json');
                            http.send();
                  }
          
          
              WebEngineView{
                  id: webEngineView
                  anchors.fill: parent
              }
          
              Component.onCompleted: {
                  var html = parsing()
                  webEngineView.loadHtml(html)
              }
          }
          
          
          R ? 2 Replies Last reply 23 May 2016, 13:44
          0
          • Q Qjay
            23 May 2016, 13:41

            Hey , thanks for the reply :) but all i am getting is blank screen :(

            P.S. i am sure that parsing() returns string since i have checked it using typeof

            here is the code :

            import QtQuick 2.5
            import QtQuick.Window 2.2
            import QtWebEngine 1.1
            import QtWebKit 3.0
            
            
            Window {
                visible: true
                width: 600
                height: 400
            
                function parsing() {
                        var http = new XMLHttpRequest();
                       var json , parse , text ;
            
                         http.onreadystatechange = function(){
                           if(http.readyState == 4 && http.status == 200)
                           { json = http.responseText;
            
                            parse = JSON.parse(json);
            
                               text = parse.parse.text["*"];
                               console.log(text);
                              return(text);
                           }
                         };
                               http.open('GET','http://en.wikitolearn.org/api.php?action=parse&page=Linear%20Algebra/Sets&format=json');
                              http.send();
                    }
            
            
                WebEngineView{
                    id: webEngineView
                    anchors.fill: parent
                }
            
                Component.onCompleted: {
                    var html = parsing()
                    webEngineView.loadHtml(html)
                }
            }
            
            
            R Offline
            R Offline
            raven-worx
            Moderators
            wrote on 23 May 2016, 13:44 last edited by
            #5

            @Qjay said:

            P.S. i am sure that parsing() returns string since i have checked it using typeof

            sorry but this means nothing.
            When i set "<html></html>" its a string but it still only displays a blank page...

            What is the content you set exactly?

            --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
            If you have a question please use the forum so others can benefit from the solution in the future

            Q 1 Reply Last reply 23 May 2016, 14:30
            0
            • Q Qjay
              23 May 2016, 13:41

              Hey , thanks for the reply :) but all i am getting is blank screen :(

              P.S. i am sure that parsing() returns string since i have checked it using typeof

              here is the code :

              import QtQuick 2.5
              import QtQuick.Window 2.2
              import QtWebEngine 1.1
              import QtWebKit 3.0
              
              
              Window {
                  visible: true
                  width: 600
                  height: 400
              
                  function parsing() {
                          var http = new XMLHttpRequest();
                         var json , parse , text ;
              
                           http.onreadystatechange = function(){
                             if(http.readyState == 4 && http.status == 200)
                             { json = http.responseText;
              
                              parse = JSON.parse(json);
              
                                 text = parse.parse.text["*"];
                                 console.log(text);
                                return(text);
                             }
                           };
                                 http.open('GET','http://en.wikitolearn.org/api.php?action=parse&page=Linear%20Algebra/Sets&format=json');
                                http.send();
                      }
              
              
                  WebEngineView{
                      id: webEngineView
                      anchors.fill: parent
                  }
              
                  Component.onCompleted: {
                      var html = parsing()
                      webEngineView.loadHtml(html)
                  }
              }
              
              
              ? Offline
              ? Offline
              A Former User
              wrote on 23 May 2016, 13:55 last edited by
              #6

              @Qjay Your're doing it wrong. Look:

              import QtQuick 2.5
              import QtQuick.Window 2.2
              import QtWebEngine 1.1
              
              
              Window {
                  visible: true
                  width: 600
                  height: 400
              
                  function parsing() {
                      var http = new XMLHttpRequest();
                      var json , parse , text ;
              
                      http.onreadystatechange = function(){
                          if(http.readyState == 4 && http.status == 200)
                          { json = http.responseText;
              
                              parse = JSON.parse(json);
              
                              text = parse.parse.text["*"];
                              console.log(text);
                              webEngineView.loadHtml(text);  // <-- LOOK HERE
                              return (text);
                          }
                      };
                      http.open('GET','http://en.wikitolearn.org/api.php?action=parse&page=Linear%20Algebra/Sets&format=json');
                      http.send();
                  }
              
              
                  WebEngineView{
                      id: webEngineView
                      anchors.fill: parent
                  }
              
                  Component.onCompleted: parsing()
              }
              
              Q 1 Reply Last reply 23 May 2016, 14:38
              0
              • R raven-worx
                23 May 2016, 13:44

                @Qjay said:

                P.S. i am sure that parsing() returns string since i have checked it using typeof

                sorry but this means nothing.
                When i set "<html></html>" its a string but it still only displays a blank page...

                What is the content you set exactly?

                Q Offline
                Q Offline
                Qjay
                wrote on 23 May 2016, 14:30 last edited by
                #7

                @raven-worx the html content is right :) I get itfrom an API . It's just that the API don't give me headers like <html> <link rel =..>

                what i get is from main div tags like : <p> something </p> .

                @raven-worx : next time i will be try to be more elaborate . Thanks for helping

                @Wieland found out i was doing wrong , he corrected it :) .

                1 Reply Last reply
                0
                • ? A Former User
                  23 May 2016, 13:55

                  @Qjay Your're doing it wrong. Look:

                  import QtQuick 2.5
                  import QtQuick.Window 2.2
                  import QtWebEngine 1.1
                  
                  
                  Window {
                      visible: true
                      width: 600
                      height: 400
                  
                      function parsing() {
                          var http = new XMLHttpRequest();
                          var json , parse , text ;
                  
                          http.onreadystatechange = function(){
                              if(http.readyState == 4 && http.status == 200)
                              { json = http.responseText;
                  
                                  parse = JSON.parse(json);
                  
                                  text = parse.parse.text["*"];
                                  console.log(text);
                                  webEngineView.loadHtml(text);  // <-- LOOK HERE
                                  return (text);
                              }
                          };
                          http.open('GET','http://en.wikitolearn.org/api.php?action=parse&page=Linear%20Algebra/Sets&format=json');
                          http.send();
                      }
                  
                  
                      WebEngineView{
                          id: webEngineView
                          anchors.fill: parent
                      }
                  
                      Component.onCompleted: parsing()
                  }
                  
                  Q Offline
                  Q Offline
                  Qjay
                  wrote on 23 May 2016, 14:38 last edited by
                  #8

                  @Wieland Thanks !! it works fine . I will now complete the QMLbook . i am still noob :/

                  Thanks for the help

                  1 Reply Last reply
                  0

                  1/8

                  23 May 2016, 11:16

                  • Login

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