Reputation: 7142
I have used QNetworkRequest
to retrieve xml off the web without problems:
request.setUrl(QUrl("http://api.somesite.com/api/4we35r/somefile.xml"));
myNetworkAccessManager->get(request);
How would I go about downloading an image? Ex:
http://www.mysite.com/27eye28/images/myimage.png
Do I just replace the xml url above with the png url? Do I have to do anything special?
Upvotes: 1
Views: 1600
Reputation: 405
Yes, replacing the URL is all that you have to do.
Here's a working example,
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();
}
}
Upvotes: 2