0

I know that QWebEngineProfile and QWebEngineDownloadItem are used in order to download something. But I don't understand how. I'm trying to use connects in order to achieve downloads. Here's my code

void MainWindow::handleDownloadSlot(QWebEngineDownloadItem *download) {
    download->accept();
}
void MainWindow::downloadRequested(QWebEngineDownloadItem *download) {
    download->accept();
}
  connect (pro,SIGNAL(downloadRequested(QWebEngineDownloadItem *)),this,SLOT(handleDownloadSlot(QWebEngineDownloadItem *)));

1 Answer 1

2

Check the Web Demo Browser example which includes an example with a Download Manager.

If you are sharing a default QWebEngineProfile, try:

    connect(QWebEngineProfile::defaultProfile(), SIGNAL(downloadRequested(QWebEngineDownloadItem*)),
                this, SLOT(downloadRequested(QWebEngineDownloadItem*)));

For a profile defined in a custom QWebEnginePage, try:

    connect(webView->page()->profile(), SIGNAL(downloadRequested(QWebEngineDownloadItem*)),
                this, SLOT(downloadRequested(QWebEngineDownloadItem*)));

Now handle your download to start:

void MainWindow::downloadRequested(QWebEngineDownloadItem* download) {
        if (download->savePageFormat() != QWebEngineDownloadItem::UnknownSaveFormat)  {
            qDebug() << "Format: " <<  download->savePageFormat();
            qDebug() << "Path: " << download->path();
            // If you want to modify something like the default path or the format
            download->setSavePageFormat(...);
            download->setPath(...);
            // Check your url to accept/reject the download
            download->accept();
        }
}

If you want to show a progress dialog with the download progress, just use signals availables in the class QWebEngineDownloadItem:

connect(download, SIGNAL(downloadProgress(qint64, qint64)), this, SLOT(setCurrentProgress(qint64, qint64)));
7
  • For some reason it doesn't work for me. I click download button and nothing happens. It doesn't even show anything in debug console Aug 8, 2016 at 8:58
  • I'm porting from QWebKit to QWebEngine. And there was signal called linkClicked. There's no such signal in WebEngine. Maybe this is the reason? Aug 8, 2016 at 9:21
  • The code is working for me in Qt 5.7. If you want to handle every single URL, check my answer in this post: stackoverflow.com/questions/38343790/…
    – mohabouje
    Aug 8, 2016 at 9:25
  • For python i just did QWebEngineView().page().profile().downloadRequested.connect(downloadFucntion) and then accepted it by downloadItem.accept() . Hope it's helpful ^
    – Pixsa
    Oct 16, 2018 at 22:09
  • @Pixsa >3 years later: would love to know how you got the download to work in Python! Trying to get it to work in github.com/MadsAndreasen/PySideFlask and failing - do you have any boilerplate or more detail? Would be hugely appreciated!
    – Jon T
    Jan 28, 2022 at 19:04

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.