How to download files from QWebView?

I created a small web browser with QT Creator and QWebView. I work very well and the pages load very fast. But how can I make my browser capable of downloading files? I looked through the list of signals and functions, but I did not find anything that could help me. How can I find out if QUrl contains a link to a file other than text / html to download it?

+4
source share
1 answer

QWebView has a QWebPage member, which you can access to this pointer using webView.page() . Here you have to look. QWebPage has two signals: downloadRequested(..) and unsupportedContent(..) . I believe that dowloadRequest is only issued when the user right-clicks the link and select "Save Link", and unsupportedContent is issued when the destination URL cannot be shown (and not html / text).

But for unsupported Content to be emitted, you must set forwardUnsupportedContent to True using the webPage.setForwardUnsupportedContent(true) function. Here is the minimal example I created:

 MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent), ui(new Ui::MainWindow) { ui->setupUi(this); ui->webView->page()->setForwardUnsupportedContent(true); connect(ui->webView->page(),SIGNAL(downloadRequested(QNetworkRequest)),this,SLOT(download(QNetworkRequest))); connect(ui->webView->page(),SIGNAL(unsupportedContent(QNetworkReply*)),this,SLOT(unsupportedContent(QNetworkReply*))); } MainWindow::~MainWindow() { delete ui; } void MainWindow::download(const QNetworkRequest &request){ qDebug()<<"Download Requested: "<<request.url(); } void MainWindow::unsupportedContent(QNetworkReply * reply){ qDebug()<<"Unsupported Content: "<<reply->url(); } 

Remember that MainWindow :: download (..) and MainWindow :: unsupportedContent (..) are SLOT !

+9
source

Source: https://habr.com/ru/post/1478858/


All Articles