Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. How can pop up be made allowed when we use javascript in Qt??
QtWS25 Last Chance

How can pop up be made allowed when we use javascript in Qt??

Scheduled Pinned Locked Moved General and Desktop
7 Posts 3 Posters 2.6k 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.
  • U Offline
    U Offline
    umaviswa
    wrote on last edited by
    #1

    function saveTextAsFile()
    {
    var textToWrite = document.getElementById("latbox").value+"\n"+document.getElementById("lngbox").value;
    var textFileAsBlob = new Blob([textToWrite], {type:'text/plain'});
    //var fileNameToSaveAs = document.getElementById("inputFileNameToSaveAs").value;
    var downloadLink = document.createElement("a");
    //downloadLink.download = fileNameToSaveAs+".txt";
    downloadLink.download = "values.txt";
    downloadLink[removed] = "Download File";
    if (window.webkitURL != null)
    {
    downloadLink.href = window.webkitURL.createObjectURL(textFileAsBlob);
    }
    else
    {
    // Firefox requires the link to be added to the DOM
    // before it can be clicked.
    downloadLink.href = window.URL.createObjectURL(textFileAsBlob);
    downloadLink.onclick = destroyClickedElement;
    downloadLink.style.display = "none";
    document.body.appendChild(downloadLink);
    }
    downloadLink.click();
    }

    here i had a button in javascript which when clicked pops up a download window asking for saveas or open. it is working outside Qt. but the pop up is blocked in Qt.
    i tried all the available setting changes nothing worked :(

    QWebSettings *settings = ui->webView_2->settings();
    settings->setAttribute(QWebSettings::JavascriptEnabled, true);
    settings->setAttribute(QWebSettings::PluginsEnabled, true);
    settings->setAttribute(QWebSettings::AutoLoadImages, true);
    settings->setAttribute(QWebSettings::JavascriptCanOpenWindows, true);
    settings->setAttribute(QWebSettings::LocalContentCanAccessFileUrls,true);
    please help . how to make it work.

    1 Reply Last reply
    0
    • T Offline
      T Offline
      thEClaw
      wrote on last edited by
      #2

      Use the code-tag for your code, please. This is way too hard to read.

      Why do you not use Qts integrated saveas-/open-dialog instead?

      1 Reply Last reply
      0
      • U Offline
        U Offline
        umaviswa
        wrote on last edited by
        #3

        Thanks for your response Mr.thEClaw. Please let me know if there is any possible way in Qt where i can save values from html to a text file??

        1 Reply Last reply
        0
        • T Offline
          T Offline
          thEClaw
          wrote on last edited by
          #4

          What exactly do you mean? Maybe have a look at "QFile":http://qt-project.org/doc/qt-5.1/qtcore/qfile.html or "QSettings":http://qt-project.org/doc/qt-5.1/qtcore/qsettings.html , depending on what you want to achieve.

          1 Reply Last reply
          0
          • raven-worxR Offline
            raven-worxR Offline
            raven-worx
            Moderators
            wrote on last edited by
            #5

            here you go:
            @
            QWebPage* page = myWebView->page();
            page->setForwardUnsupportedContent(true); //<-- IMPORTANT!
            connect(page, SIGNAL(downloadRequested(const QNetworkRequest &)), this, SLOT(onDownload(const QNetworkRequest &)));
            connect(page, SIGNAL(unsupportedContent(QNetworkReply*)), this, SLOT(onUnsupportedContent(QNetworkReply*)));

            ...

            //SLOT
            void onDownload(const QNetworkRequest & request)
            {
            //this slot is called when the user right-clicks on an element and wants to save it, i'm not sure if it may be triggered also in your case
            }

            //SLOT
            void onUnsupportedContent(QNetworkReply* reply)
            {
            //this slot is called when the user should be prompted to download a file, so this is most probably the way to go for you
            }
            @

            --- 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
            • U Offline
              U Offline
              umaviswa
              wrote on last edited by
              #6

              <!DOCTYPE html>
              <html>
              <head>
              <meta name="viewport" content="initial-scale=1.0, user-scalable=no">
              <meta charset="utf-8">
              <title>Ground Overlays</title>
              <link href="default.css" rel="stylesheet">
              <div id="latlong">
              <p>Latitude: <input size="20" type="text" id="latbox" name="lat" >Longitude: <input size="20" type="text" id="lngbox" name="lng" ></p>
              </div>
              <table>
              <tr>
              <td><button >Save Text to File</button></td>
              </tr>
              </table>
              [removed][removed]
              [removed]
              function destroyClickedElement(event)
              {
              document.body.removeChild(event.target);
              }
              function saveTextAsFile()
              {
              var textToWrite = document.getElementById("latbox").value+"\n"+document.getElementById("lngbox").value;
              var textFileAsBlob = new Blob([textToWrite], {type:'text/plain'});
              //var fileNameToSaveAs = document.getElementById("inputFileNameToSaveAs").value;
              var downloadLink = document.createElement("a");
              //downloadLink.download = fileNameToSaveAs+".txt";
              downloadLink.download = "values.txt";
              downloadLink[removed] = "Download File";
              if (window.webkitURL != null)
              {
              downloadLink.href = window.webkitURL.createObjectURL(textFileAsBlob);
              }
              else
              {
              // Firefox requires the link to be added to the DOM
              // before it can be clicked.
              downloadLink.href = window.URL.createObjectURL(textFileAsBlob);
              downloadLink.onclick = destroyClickedElement;
              downloadLink.style.display = "none";
              document.body.appendChild(downloadLink);
              }
              downloadLink.click();
              }
              function initialize() {
              var newark = new google.maps.LatLng(25,85);
              var imageBounds = new google.maps.LatLngBounds(
              new google.maps.LatLng(23.7407,82.4815),
              new google.maps.LatLng(26.2593,87.5185));
              var mapOptions = {
              zoom: 5,
              center: newark,
              mapTypeId: google.maps.MapTypeId.SATELLITE
              }
              var map = new google.maps.Map(document.getElementById('map-canvas'), mapOptions);
              var oldmap = new google.maps.GroundOverlay(
              'file:///root/Documents/fwdjavascriptwritingvaluestoatextfile (copy)/wind10.jpg',
              imageBounds);
              oldmap.setMap(map);
              var marker = new google.maps.Marker({
              draggable: true,
              position: newark,
              map: map,
              title: "Your location"
              });
              google.maps.event.addListener(marker, 'dragend', function (event) {
              document.getElementById("latbox").value = this.getPosition().lat();
              document.getElementById("lngbox").value = this.getPosition().lng();
              });
              }
              [removed]
              </head>
              <body >
              <div id="map-canvas"></div>
              </body>
              </html>
              this is my html code.

              in Qt i run this as

              QString map_path = QDir::currentPath()+"/click.htm";
              map_path = "file://"+map_path;
              ui->webView_2->setUrl(QUrl(map_path));
              ui->webView_2->update();

              normally if i run html file and click on save as button there appears a pop up asking to saveas or open with.

              but in Qt webview no window is appeared even if i click on save as button.

              please help.

              1 Reply Last reply
              0
              • raven-worxR Offline
                raven-worxR Offline
                raven-worx
                Moderators
                wrote on last edited by
                #7

                first of all: use CODE TAGS!!!

                second: did you read and tried anything of my previous post?!!

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

                • Login

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