QT显示从网络访问管理器获取的图像
我正在尝试显示从使用网络访问管理器发出的获取请求中获得的图像。我能够编译甚至能够运行它。但我无法在 Qlabel 中显示图像。
QNetworkAccessManager* nam;
void MainWindow::on_pushButton_clicked()
{
nam = new QNetworkAccessManager(this);
QUrl url("http://i.imgur.com/Uw7Fk.jpg");
QNetworkReply* reply = nam->get(QNetworkRequest(url));
if (reply->error() == QNetworkReply::NoError)
{
QImageReader imageReader(reply);
imageReader.setAutoDetectImageFormat (false);
QImage pic = imageReader.read();
ui->label_2->setPixmap(QPixmap::fromImage(pic));
}
}
请告诉我哪里出错了。
I am trying to display the image obtained from the get request made using networkaccess manager. I am able to compile and even able to run it. but I am unable to show the image in a Qlabel.
QNetworkAccessManager* nam;
void MainWindow::on_pushButton_clicked()
{
nam = new QNetworkAccessManager(this);
QUrl url("http://i.imgur.com/Uw7Fk.jpg");
QNetworkReply* reply = nam->get(QNetworkRequest(url));
if (reply->error() == QNetworkReply::NoError)
{
QImageReader imageReader(reply);
imageReader.setAutoDetectImageFormat (false);
QImage pic = imageReader.read();
ui->label_2->setPixmap(QPixmap::fromImage(pic));
}
}
Please tell me where I am going wrong.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
调用
QNetworkAccessManager::get()
后,QNetworkReply
中的数据尚未准备好。该调用是异步的,在尝试检索任何数据之前,您需要连接到 QNetworkAccessManager 的finished()
信号或 QNetworkReply 的readyRead()
信号。The data in
QNetworkReply
is not ready immediately after the call toQNetworkAccessManager::get()
. The call is asynchronous, and you need to connect to either thefinished()
signal of QNetworkAccessManager, orreadyRead()
signal of QNetworkReply before you attempt to retrieve any data.要同步获取图像,您可以使用 QEventLoop,如下所示:
To get image synchronously, you can use QEventLoop like below: