Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. QtWebEngine
  4. How to redirect QWebEngineUrlRequestInfo to local files?

How to redirect QWebEngineUrlRequestInfo to local files?

Scheduled Pinned Locked Moved Unsolved QtWebEngine
1 Posts 1 Posters 930 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
    madasionka
    wrote on last edited by
    #1

    I have a simple Qt application that loads a page in a QWebEngineView. I would like to redirect all http requests with the word "static" in the url to local files. Using WebUrlRequestInterceptor I reimplemented the interceptRequest method. Here is the code:

    class MyWebUrlRequestInterceptor : public QWebEngineUrlRequestInterceptor
    {
    public:
        void interceptRequest(QWebEngineUrlRequestInfo &info) {
            if (info.requestUrl().toString().contains("static")) {
                QString newUrl = QDir::currentPath() + info.requestUrl().toString().mid(21, info.requestUrl().toString().length());
                qDebug() << "new url = " << newUrl;
                info.redirect(QUrl::fromLocalFile(newUrl));
            }
        }
    };
    

    In main function I did

    MyWebUrlRequestInterceptor *wuri = new MyWebUrlRequestInterceptor();
    QWebEngineProfile::defaultProfile()->setRequestInterceptor(wuri);
    

    And the interceptRequest seems to work fine, but I get a message.

    Not allowed to load local resource

    I searched online and I many people are saying that I should add the --disable-web-security flag. So i did in my .pro file:

    QMAKE_CXXFLAGS += --disable-web-security
    

    But it doesn't seem to be working. Am I doing something wrong? Is there a different solutions? Could I make custom protocols to serve local files and redirect to them in Qt as a workaround?

    I'm using Qt 5.9.1 and QtCreator 4.3.1.

    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