31

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.

Angie Quijano
  • 4,167
  • 3
  • 25
  • 30
StickyCube
  • 1,721
  • 2
  • 17
  • 22
  • We had to make a similar decision and chose to stay with webkit until the blink integration is more complete. There is also missing request modification via QNetworkManager and no printing yet. For an example how to solve this with Qt 5.5, have a look at the [otter browser](https://github.com/OtterBrowser/otter-browser/blob/master/src/modules/backends/web/qtwebengine/QtWebEngineWebWidget.cpp#L122) and the [Qt sources](http://code.qt.io/cgit/qt/qtwebengine.git/tree/src/webenginewidgets/api/qwebengineprofile.h#n106). – elsamuko Jun 26 '15 at 20:23
  • @StickyCube Can you please let me know how have you launched the webengine? I'm trying to use quicknanobrowser as ./quicknanobrowser -platform wayland. But, the app is crashing when playing the youtube videos. Please post me if you have any suggestion. – surendra nath Sep 22 '17 at 08:05

1 Answers1

7

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

Xian Nox
  • 368
  • 2
  • 11