从QWebView类中下载图片

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
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值