如何在QWebEngine中处理下载?

时间:2016-08-07 09:32:59

标签: c++ qt downloading

我知道QWebEngineProfile和QWebEngineDownloadItem用于下载内容。但我不明白怎么做。我尝试使用连接以实现下载。这是我的代码

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

1 个答案:

答案 0 :(得分:2)

查看包含下载管理器示例的Web Demo Browser示例。

如果您要共享默认QWebEngineProfile,请尝试:

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

对于自定义QWebEnginePage中定义的配置文件,请尝试:

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

现在处理下载开始:

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();
        }
}

如果您想显示包含下载进度的进度对话框,只需使用课程QWebEngineDownloadItem中的可用信号:

connect(download, SIGNAL(downloadProgress(qint64, qint64)), this, SLOT(setCurrentProgress(qint64, qint64)));