Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. Qt WebKit
  4. [Solved] Read local file from QWebView using Ajax request
Forum Update on Monday, May 27th 2025

[Solved] Read local file from QWebView using Ajax request

Scheduled Pinned Locked Moved Qt WebKit
8 Posts 2 Posters 5.8k 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.
  • M Offline
    M Offline
    Maxbester
    wrote on 11 Jan 2013, 14:57 last edited by
    #1

    Hi,

    I am developing a Qt/C++ program which encapsulates an existing HTML5/JQuery web app.

    I used to make Ajax requests to read files from a server. But now, I would like Qt to read a file from the local disk and send its content to my web app.

    I think I need Qt to catch Ajax requests from the web app and return the file content as the Ajax request result.

    The problem is I don't know how to do. For now, I've not found anything about that on google.

    Thanks!

    1 Reply Last reply
    0
    • A Offline
      A Offline
      AcerExtensa
      wrote on 11 Jan 2013, 15:33 last edited by
      #2

      You need to subclass QWebPage and reimplement "acceptNavigationRequest":http://qt-project.org/doc/qt-5.0/qtwebkit/qwebpage.html#acceptNavigationRequest function. You can catch requests and reroute them to needed location like file:// etc...

      God is Real unless explicitly declared as Integer.

      1 Reply Last reply
      0
      • M Offline
        M Offline
        Maxbester
        wrote on 11 Jan 2013, 16:19 last edited by
        #3

        [quote author="AcerExtensa" date="1357918430"]You need to subclass QWebPage and reimplement "acceptNavigationRequest":http://qt-project.org/doc/qt-5.0/qtwebkit/qwebpage.html#acceptNavigationRequest function. You can catch requests and reroute them to needed location like file:// etc...[/quote]

        Ok thanks a lot, I am going to have a look at this solution.

        1 Reply Last reply
        0
        • M Offline
          M Offline
          Maxbester
          wrote on 14 Jan 2013, 07:58 last edited by
          #4

          [quote author="AcerExtensa" date="1357918430"]You need to subclass QWebPage and reimplement "acceptNavigationRequest":http://qt-project.org/doc/qt-5.0/qtwebkit/qwebpage.html#acceptNavigationRequest function. You can catch requests and reroute them to needed location like file:// etc...[/quote]

          Actually, I can't get into it. Could you write an example of this solution?

          First of all, I reimplemented acceptNavigationRequest in a QWebPage subclass but it doesn't catch Ajax requests...

          Here is my reimplementation:

          @bool MyQWebPage::acceptNavigationRequest(QWebFrame * frame, const QNetworkRequest & request, NavigationType type)
          {
          QUrl url = request.url();
          QString path = url.path();

          printf("Path is %s and navigation is %d\n", path.toLocal8Bit().constData(), type);
          
          if (type == QWebPage::NavigationTypeOther)
          {
          
              if(path == "...")
              {
                    ...
              }
              else if(...)
              {
                    ...
              }
          }
          return true;
          

          }@

          Thanks

          1 Reply Last reply
          0
          • A Offline
            A Offline
            AcerExtensa
            wrote on 14 Jan 2013, 08:17 last edited by
            #5

            Your subclass of QWebPage
            @
            class ExWebPage : public QWebPage;
            bool ExWebPage::acceptNavigationRequest(QWebFrame * frame, const QNetworkRequest & request, NavigationType type)
            {
            /* do what you need here. */
            return true;
            }
            @

            Set your page to the webview:
            @
            QWebView * view = new QWebView(this);
            ExWebPage * page = new ExWebPage();
            view->setPage(page);
            @

            bq. I think I know how to catch the request but how should I reroute it?

            for example: you know name of files you need to catch, then in acceptNavigationRequest check request url and if it contains request to that file simply change request URL to the URL where this file should be loaded... for example from file:// url shema for local file access. You will also need to set QWebSettings::LocalContentCanAccessRemoteUrls to true.

            But you should know it better how do you should catch and where you should reroute it....

            God is Real unless explicitly declared as Integer.

            1 Reply Last reply
            0
            • A Offline
              A Offline
              AcerExtensa
              wrote on 14 Jan 2013, 08:21 last edited by
              #6

              In the worst case you can change QNAM of webview to your own, so you can catch any data connections... search this forum for it, some time ago we have made sniffer in this manner...

              God is Real unless explicitly declared as Integer.

              1 Reply Last reply
              0
              • M Offline
                M Offline
                Maxbester
                wrote on 14 Jan 2013, 09:23 last edited by
                #7

                Well I did subclass QNAM and it works fine. But I don't understand why reimplementing acceptNavigationRequest doesn't work with Ajax requests.

                1 Reply Last reply
                0
                • A Offline
                  A Offline
                  AcerExtensa
                  wrote on 14 Jan 2013, 09:34 last edited by
                  #8

                  I think acceptNavigationRequest is only called on user interaction and javascript/ajax bypass it, so you can catch it in createRequest of QNAM....

                  God is Real unless explicitly declared as Integer.

                  1 Reply Last reply
                  0

                  1/8

                  11 Jan 2013, 14:57

                  • 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