qt http下载图片和mp3文件
时间: 2024-06-14 16:03:18 浏览: 150
以下是使用Qt进行http下载图片和mp3文件的示例代码:
1. 下载图片
```cpp
QUrl url("https://example.com/image.png");
QNetworkAccessManager manager;
QNetworkReply *reply = manager.get(QNetworkRequest(url));
QEventLoop loop;
QObject::connect(reply, &QNetworkReply::finished, &loop, &QEventLoop::quit);
loop.exec();
if (reply->error() == QNetworkReply::NoError) {
QByteArray data = reply->readAll();
QPixmap pixmap;
pixmap.loadFromData(data);
// 将pixmap保存到本地文件
pixmap.save("image.png");
}
reply->deleteLater();
```
2. 下载mp3文件
```cpp
QUrl url("https://example.com/song.mp3");
QNetworkAccessManager manager;
QNetworkReply *reply = manager.get(QNetworkRequest(url));
QEventLoop loop;
QObject::connect(reply, &QNetworkReply::finished, &loop, &QEventLoop::quit);
loop.exec();
if (reply->error() == QNetworkReply::NoError) {
QByteArray data = reply->readAll();
QFile file("song.mp3");
if (file.open(QIODevice::WriteOnly)) {
file.write(data);
file.close();
}
}
reply->deleteLater();
```
阅读全文