Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to download files from QWebView?

Tags:

c++

qt

I created a small web browser with QT Creator and QWebView. I's working very good and the pages are loading very fast. But how can I make my browser capable of downloading files? I looked through the signals and functions list, but I did't find something that could help me. How can I found out if a QUrl contains a link to a file other than text/html so I can download it?

like image 569
StefanEuSunt Avatar asked May 02 '13 19:05

StefanEuSunt


1 Answers

QWebView has a 'QWebPage' member which you can access it's pointer with webView.page() . This is where you should look. QWebPage has two signals: downloadRequested(..) and unsupportedContent(..). I believe dowloadRequest is only emitted when user right clicks a link and selects 'Save Link' and unsupportedContent is emitted when target URL cannot be shown (not an html/text).

But for unsupportedContent to be emitted, you should set forwardUnsupportedContent to True with function webPage.setForwardUnsupportedContent(true). Here is a minimal example I have 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, MainWindow::download(..) and MainWindow::unsupportedContent(..) are SLOTs !

like image 152
HeyYO Avatar answered Oct 14 '22 21:10

HeyYO