Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Handling Downloads in QtWebEngine

We are currently migrating a project from the QtWebkit to the QWebEngine. However, handling downloads is causing a bit of a headache. Previously we handled this using the QWebPage::unsupportedContent signal, like so:

QWebPage* webPage = new QWebPage(this);

QObject::connect(webPage, &QWebPage::unsupportedContent, [] (QNetworkReply* reply) {
    // do stuff with the reply
    reply->readAll();
});

When using QtWebEngine, the only thing I can think of is to use the QWebEngineView::urlChanged signal to make a request to the server and i'm not even sure if that will work.

QNetworkAccessManager* accessManager = new QNetworkAccessManager(this);
QWebEngineView* webView = new QWebEngineView(this);

QObject::connect(webView, &QWebEngineView::urlChanged, [=] (const QUrl& url) {
    if (url.path().endsWith("some_endpoint_which_results_in_a_download") {
        QNetworkReply* reply = accessManager->get(url);
        // do the same stuff to the reply
        reply->readAll();
    }
})

Obviously this approach is very limiting in that the endpoints which result in a download must be hard-coded into the application. However, I cant see a better solution. Has anyone come up with anything better?

-- Update --

The Docs from Qt's 5.5 Release plan outlines, among other improvements to the developer's control over the web cache and cookies, the following feature.

Added API for managing downloading of files

5.5 beta is intended for release on 09/04/2015 and the final for release on 26/05/2015.

To prevent any further head trauma, it may be worth just waiting for these improvements.

Having said that, I'd still be interested in a cleaner solution than mine if anyone has one.

like image 736
StickyCube Avatar asked Mar 23 '15 14:03

StickyCube


1 Answers

QtWebEngine manages downloads via the QWebEngineProfile class with the downloadRequested signal.

like image 89
Xian Nox Avatar answered Oct 30 '22 17:10

Xian Nox