1

我曾经QNetworkRequest从网上检索 xml 没有问题:

request.setUrl(QUrl("http://api.somesite.com/api/4we35r/somefile.xml"));

myNetworkAccessManager->get(request);

我将如何下载图像?前任:

http://www.mysite.com/27eye28/images/myimage.png

我只是用 png url 替换上面的 xml url 吗?我需要做一些特别的事情吗?

4

1 回答 1

2

是的,您只需更换 URL。

这是一个工作示例,

void MainWindow::GetImage(QString url)
{
    QNetworkAccessManager* manager = new QNetworkAccessManager(this);
    connect(manager, SIGNAL(finished(QNetworkReply *)), this, SLOT(replyFinished(QNetworkReply *)));

    QUrl url = QUrl(url);

    QNetworkRequest request(url);

    manager->get(request);
}

void MainWindow::replyFinished(QNetworkReply *reply)
{
    if(reply->error() != QNetworkReply::NoError)
    {
        ui->textBrowser->setText("Error: " +  reply->errorString());
    }
    else
    {
        QByteArray responseData = reply->readAll();
        QFile file("d:\\myImage.png");
        file.open(QIODevice::WriteOnly);
        file.write((responseData));
        file.close();
    }
}
于 2013-08-08T13:31:12.450 回答