How to redirect QWebEngineUrlRequestInfo to local files?



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


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.