How to port QNetworkAccessManager to WebEngine?

I know that Qt WebEngine does not interact with QNetworkAccessManager. But how do you port this code from 5.5 to 5.7 in order for it to work?

QNetworkAccessManager *p =  getView()->page()->networkAccessManager();

networkAccessManager is highlited and error says "class QWebEnginePage has no member networkAccessManager"

Upvotes: 1

Views: 2672

Answers (3)

Valentin H
Valentin H

Reputation: 7458

Maybe this helps:

Qt WebEngine does not interact with QNetworkAccessManager

Some classes of Qt Network like QAuthenticator were reused for their interface but, unlike Qt WebKit, Qt WebEngine has its own HTTP implementation and cannot go through a QNetworkAccessManager. The signals and methods of QNetworkAccessManager that are still supported were moved to the QWebEnginePage class.

Qt WebKit:

QNetworkAccessManager qnam;
QWebPage page;
page.setNetworkAccessManager(&qnam);
connect(&qnam, SIGNAL (authenticationRequired(QNetworkReply*, QAuthenticator*)), this, SLOT (authenticate(QNetworkReply*, QAuthenticator*)));

Qt WebEngine:

QWebEnginePage page;
connect(&page, SIGNAL (authenticationRequired(QUrl, QAuthenticator*)), this, SLOT (authenticate(QUrl, QAuthenticator*)));

Source: https://wiki.qt.io/Porting_from_QtWebKit_to_QtWebEngine

Upvotes: 2

mohabouje
mohabouje

Reputation: 4050

I found a solution, to share Cookies with QWebEngine and QNetworkAccesManager by using QWebEngineCookieStore. Subclass QNetworkCookieJar:

class CookieWebEngine : public QNetworkCookieJar
{
.....
protected:
    // Reimplement this functions to work with your _cookies list;
    bool insertCookie(const QNetworkCookie &cookie);
    bool deleteCookie(const QNetworkCookie &cookie);
    bool updateCookie(const QNetworkCookie &cookie);
    bool validateCookie(const QNetworkCookie &cookie, const QUrl &url) const;
private:
    // Save Chromium Cookies
    QWebEngineCookieStore *_store;
    // Save available cookies
    QList<QNetworkCookie> _cookies;
}

Now, lets implement a function to load/save cookies in a file:

// Load Chromium Cookies
void CookieWebEngine::loadStore() {
    // Save cookies profile shared
    QWebEngineProfile::defaultProfile()->setPersistentCookiesPolicy(QWebEngineProfile::ForcePersistentCookies);
    _store = WebEngineProfile::defaultProfile()->cookieStore();
    connect(_store, &QWebEngineCookieStore::cookieAdded, this, &CookieWebEngine::handleCookieAdded);
    _store->loadAllCookies();
}
// Load/Save cookies in arraylist in a file
void CookieWebEngine::load() {

    // load cookies and exceptions
    qRegisterMetaTypeStreamOperators<QList<QNetworkCookie> >("QList<QNetworkCookie>");
    const QString location = cookiesDirectory() + COOKIES_FILE;
    QSettings cookieSettings(location, QSettings::IniFormat);
    _cookies = qvariant_cast<QList<QNetworkCookie> >(cookieSettings.value(QLatin1String("cookies")));
    setAllCookies(_cookies);
    // Now user iterate and add it to chromium
    for (auto cookie : _cookies) {
       _store->setCookie(cookie);
    }
    cookieSettings.sync();
}

void CookieWebEngine::save()
{
    QString directory = cookiesDirectory();
    if (!QFile::exists(directory)) {
        QDir dir;
        dir.mkpath(directory);
    }
    const QString location = directory + COOKIES_FILE;
    QSettings cookieSettings(location, QSettings::IniFormat);
    cookieSettings.setValue(QLatin1String("cookies"), QVariant::fromValue<QList<QNetworkCookie>>(_cookies));
    cookieSettings.sync();
}

Now, just connect and handle cookies loaded from the webview:

void CookieWebEngine::handleCookieAdded(const QNetworkCookie &cookie)
{
    if (insertCookie(cookie)) {
        qDebug() << "Handle cookie " << cookie;
    }
}

Its working well for me. Now, i use chromium to sign in. After, i save my session cookie in the customized cookiejar and I use it in my customized QNetworkAccesManager.

You should update all your network manager. You could i subclassing QWebEnginePage and add an slot to update your QNetworkAccesManager and the funcition which will return your NetworkAccesManager

Upvotes: 4

evilruff
evilruff

Reputation: 4085

There is no way to port it the way you describe, however it's possible to bring QtWebKit from 5.5.1 to 5.7.. it will require some 'fine-tuning' in compile options but it's not really difficult and works very stable including QML support.

Upvotes: 0

Related Questions