从QWebView类中下载图片

来源:互联网 发布:淘宝网的网址域名 编辑:程序博客网 时间:2024/05/17 05:50

forked from :http://www.qtcentre.org/threads/41997-Qwebview-Copy-Text-And-Save-Image



Thanks arturo182.

That worked great. And im greatfull for you putting me in the right direction for the image downloading.

Below is the full code for anyone looking at this thread. I hope it helps someone as this task has wasted me 2 days

Header File

Qt Code:


private slots:    void downloadRequested(const QNetworkRequest &); //used for save image    void downloadingIssueFinished(); //used for save image    void keyPressEvent(QKeyEvent *e); //used for crtl-c


cpp file - the below code was taken from http://www.linuxjournal.com/magazine...op-application

this code is for the same image bit of the fix

Qt Code:
void frm_web::downloadRequested(        const QNetworkRequest &request){  // First prompted with a file dialog to make sure  // they want the file and to select a download  // location and name.  QString defaultFileName =    QFileInfo(request.url().toString()).fileName();  QString fileName =    QFileDialog::getSaveFileName(this,                                 tr("Save File"),                                 defaultFileName);  if (fileName.isEmpty())    return;  // Construct a new request that stores the  // file name that should be used when the  // download is complete  QNetworkRequest newRequest = request;  newRequest.setAttribute(QNetworkRequest::User,                          fileName);  // Ask the network manager to download  // the file and connect to the progress  // and finished signals.  QNetworkAccessManager *networkManager =    ui->webView->page()->networkAccessManager();  QNetworkReply *reply =    networkManager->get(newRequest);  //connect(    //reply, SIGNAL(downloadProgress(qint64, qint64)),    //this, SLOT(downloadProgress(qint64, qint64))); connect(reply, SIGNAL(finished()),     this, SLOT(downloadingIssueFinished()));}void frm_web::downloadingIssueFinished(){  QNetworkReply *reply = ((QNetworkReply*)sender());  QNetworkRequest request = reply->request();  QVariant v =    request.attribute(QNetworkRequest::User);  QString fileName = v.toString();  QFile file(fileName);  if (file.open(QFile::ReadWrite))    file.write(reply->readAll());}

This is for the Ctrl-C Bit

Qt Code:
void frm_web::keyPressEvent(QKeyEvent *e){       if((e->key() == Qt::Key_C) && (e->modifiers().testFlag(Qt::ControlModifier))) {    qApp->clipboard()->setText(ui->webView->selectedText());    }}


And to connect the signals and slots

Qt Code:

connect(ui->webView->page(),        SIGNAL(downloadRequested(QNetworkRequest)),        this, SLOT(downloadRequested(QNetworkRequest)));





0 0