Intercept QML Webview request



  • I was hoping someone could help me with this. I would like to intercept a url that is set in QML webview to inject particular headers. I am subclassing QQmlNetworkAccessManagerFactory. (I use Qt5.7) but it does not seem to work. I never get inside createRequest method.
    Here's my code:

    networkaccessmanager.cpp:
    #include "networkaccessmanager.h"

    NetworkAccessManager::NetworkAccessManager(QObject *parent) :
    QNetworkAccessManager(parent)
    {
    }

    QNetworkReply* NetworkAccessManager::get(const QNetworkRequest &request) {
    qDebug() << "Url path: " << request.url().path();
    return NetworkAccessManager::get(request);
    }

    QNetworkReply* NetworkAccessManager::createRequest(Operation op, const QNetworkRequest &req, QIODevice outgoingData) {
    qDebug() << "Create request"; <------ never get here
    QNetworkReply
    reply = QNetworkAccessManager::createRequest(op, req, outgoingData);
    return reply;
    }

    NetworkAccessManagerFactory::NetworkAccessManagerFactory(QObject *parent) :
    QObject(parent)
    {
    }

    QNetworkAccessManager *NetworkAccessManagerFactory::create(QObject *parent)
    {
    NetworkAccessManager *nam = new NetworkAccessManager(parent);
    return nam;
    }

    networkaccessmanager.h:
    #ifndef NETWORKACCESSMANAGER_H
    #define NETWORKACCESSMANAGER_H

    #include <QQmlNetworkAccessManagerFactory>
    #include <QNetworkReply>
    #include <QNetworkRequest>

    class NetworkAccessManager : public QNetworkAccessManager
    {
    Q_OBJECT
    public:
    NetworkAccessManager(QObject parent = 0);
    QNetworkReply
    get(const QNetworkRequest &request);
    protected:
    virtual QNetworkReply* createRequest(Operation op, const QNetworkRequest &req, QIODevice *outgoingData = Q_NULLPTR);
    };

    class NetworkAccessManagerFactory : public QObject, public QQmlNetworkAccessManagerFactory
    {
    Q_OBJECT
    public:
    explicit NetworkAccessManagerFactory(QObject parent = 0);
    virtual QNetworkAccessManager
    create(QObject parent);
    private:
    QNetworkAccessManager
    m_networkManager;
    };
    #endif // NETWORKACCESSMANAGER_H

    main.cpp:
    #include <QGuiApplication>
    #include <QQmlApplicationEngine>
    #include <QQmlComponent>
    #include <QtWebView>

    #include "networkaccessmanager.h"

    int main(int argc, char *argv[])
    {
    QGuiApplication a(argc, argv);

    QtWebView::initialize();
    
    QQmlApplicationEngine engine;
    engine.setNetworkAccessManagerFactory(new NetworkAccessManagerFactory);
    
    QQmlComponent component(&engine, QUrl("qrc:/webView.qml"));
    if (component.status() != component.Ready) {
        qWarning() << "Error:"+ component.errorString();
    }
    
    QObject* object = component.create();
    if (object == 0) {
        qWarning() << "Qml component was not created";
    }
    
    return a.exec();
    

    }

    webView.qml:
    import QtQuick 2.2
    import QtQuick.Controls 2.0
    import QtWebView 1.1
    import QtQuick.Layouts 1.1
    import QtQuick.Controls.Styles 1.2

    ApplicationWindow {
    flags: Qt.FramelessWindowHint
    visible: true
    width: 500
    height: 500
    WebView {
    id: webView
    anchors.fill: parent
    objectName: "webView"
    url: "https://google.ca"
    }
    }


Log in to reply
 

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