下面这段代码是我从自己的项目里复制过来的,着重是QEventLoop的用法和使用场景“
//下载网络图片
void shopsubmit::setNetworkPic(QString szUrl)
{
QUrl url(szUrl);
QNetworkAccessManager manager;
QEventLoop loop;
//qDebug() << "Reading picture form " << url;
QNetworkReply *reply = manager.get(QNetworkRequest(url));
//请求结束并下载完成后,退出子事件循环
QObject::connect(reply, SIGNAL(finished()), &loop, SLOT(quit()));
//开启子事件循环
loop.exec();
if(reply->error() == QNetworkReply::NoError)
{
QByteArray jpegData = reply->readAll();
QPixmap pixmap;
pixmap.loadFromData(jpegData);
QPixmap retmap=pixmap.scaled(300,280);
ui.shImageLabel->setPixmap(retmap); // 你在QLabel显示图片
}else{
qDebug()<<"error";
}
}