How to download files from QWebView?

前端 未结 1 1344
星月不相逢
星月不相逢 2021-01-13 10:47

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

相关标签:
1条回答
  • 2021-01-13 10:57

    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 !

    0 讨论(0)
提交回复
热议问题