QT显示从网络访问管理器获取的图像

发布于 2024-11-16 15:51:53 字数 608 浏览 3 评论 0原文

我正在尝试显示从使用网络访问管理器发出的获取请求中获得的图像。我能够编译甚至能够运行它。但我无法在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

往昔成烟 2024-11-23 15:51:53

调用 QNetworkAccessManager::get() 后,QNetworkReply 中的数据尚未准备好。该调用是异步的,在尝试检索任何数据之前,您需要连接到 QNetworkAccessManager 的 finished() 信号或 QNetworkReply 的 readyRead() 信号。

The data in QNetworkReply is not ready immediately after the call to QNetworkAccessManager::get(). The call is asynchronous, and you need to connect to either the finished() signal of QNetworkAccessManager, or readyRead() signal of QNetworkReply before you attempt to retrieve any data.

鱼窥荷 2024-11-23 15:51:53

要同步获取图像,您可以使用 QEventLoop,如下所示:

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));

    QEventLoop eventloop;
    connect(reply,SIGNAL(finished()),&eventloop,SLOT(quit()));
    eventLoop.exec();

    if (reply->error() == QNetworkReply::NoError)
    {
        QImageReader imageReader(reply);
        imageReader.setAutoDetectImageFormat (false);
        QImage pic = imageReader.read();
        ui->label_2->setPixmap(QPixmap::fromImage(pic));

     }


}

To get image synchronously, you can use QEventLoop like below:

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));

    QEventLoop eventloop;
    connect(reply,SIGNAL(finished()),&eventloop,SLOT(quit()));
    eventLoop.exec();

    if (reply->error() == QNetworkReply::NoError)
    {
        QImageReader imageReader(reply);
        imageReader.setAutoDetectImageFormat (false);
        QImage pic = imageReader.read();
        ui->label_2->setPixmap(QPixmap::fromImage(pic));

     }


}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文