Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

[solved] get and save Image from URL is not working



  • I have two list of images to be downloaded from network. I use qnetworkaccessmanager to get image from url. But on the reply slot there is no image data with the reply. :( I am not able to figure out where am I going wrong.. If someone is able to figure out, pl reply...

    @ void SyncDialog::getImages()
    {
    qDebug() << Q_FUNC_INFO << "Invoked";

    int groupMasterCount = mSyncMasterData.groupMasterList.count();
    qDebug() << Q_FUNC_INFO << "groupmastercout" << groupMasterCount;
    
    for (int i = 0 ; i < groupMasterCount; ++i)
    {
        GroupMaster groupItem = mSyncMasterData.groupMasterList.at(i);
        QNetworkReply *reply =
                mImageGetNwMgr.get(QNetworkRequest(QUrl(groupItem.image)));
        reply->setProperty("name", QVariant("G_" + groupItem.groupCode));
    
        connect(reply, SIGNAL(readyRead()), this, SLOT(saveImage()));
    
        qDebug() << Q_FUNC_INFO << "get call reply" << reply->readAll();
        qDebug() << Q_FUNC_INFO << "get url" << groupItem.image;
    
        mSyncMasterData.groupMasterList[i].image.clear();
    #ifdef Q_OS_WIN
        mSyncMasterData.groupMasterList[i].image = "C:/POS/Images/G_"
                                                        +groupItem.groupCode;
    #else
        mSyncMasterData.groupMasterList[i].image = "/mnt/sdcard/POS/Images/G_"
                                                        +groupItem.groupCode;
    #endif
    
    }
    
    int itemMasterCount = mSyncMasterData.itemMasterList.count();
    qDebug() << Q_FUNC_INFO << "itemmastercout" << itemMasterCount;
    
    for (int i = 0 ; i < itemMasterCount; ++i)
    {
        ItemMaster item = mSyncMasterData.itemMasterList.at(i);
        QNetworkReply *reply =
                mImageGetNwMgr.get(QNetworkRequest(QUrl(item.imagePath)));
        reply->setProperty("name", QVariant("I_" + item.itemCode));
    
        connect(reply, SIGNAL(readyRead()), this, SLOT(saveImage()));
    
        qDebug() << Q_FUNC_INFO << "get call reply" << reply->readAll();
        qDebug() << Q_FUNC_INFO << "get url" << item.imagePath;
    
        mSyncMasterData.itemMasterList[i].imagePath.clear();
    #ifdef Q_OS_WIN
        mSyncMasterData.itemMasterList[i].imagePath = "C:/POS/Images/I_"
                                                        +item.itemCode;
    #else
        mSyncMasterData.itemMasterList[i].imagePath = "/mnt/sdcard/POS/Images/G_"
                                                        +item.itemCode;
    #endif
    
    }
    
    qDebug() << Q_FUNC_INFO << "Exits";
    }
    

    @
    In my slot I save images , but in reply->readAll gives me ""

    @void SyncDialog::saveImage()
    {
    qDebug() << Q_FUNC_INFO << "Invoked";
    
    QObject *senderObj = sender();
    QNetworkReply *reply = qobject_cast<QNetworkReply*>(senderObj);
    
    QImage* img2 = new QImage();
    img2->loadFromData(reply->readAll());
    qDebug() << Q_FUNC_INFO << "image nw reply" << reply->readAll();
    QString imageName = reply->property("name").toString();
    qDebug() << Q_FUNC_INFO << "imageName" << imageName;
    
    if(img2->isNull())
    {
        qDebug() << Q_FUNC_INFO << "image is null";
        return;
    }
    
    #ifdef Q_OS_WIN
    img2->save("C:/POS/Images/" + imageName);
    #else
    img2->save("/mnt/sdcard/POS/Images/" + imageName);
    #endif
    
    qDebug() << Q_FUNC_INFO << "Exits";
    }@
    

    Also I see in logs "libpng error: Read Error" and the slot is invoked multiple times...


  • Qt Champions 2017

    Is the image url correct ? Is the slot called ? Is it network url ? Are able to access this URL from browser ?



  • Thanks all,

    I resolved the issue with few changes :)

    @connect(reply, SIGNAL(finished()), this, SLOT(saveImage()));@
    

    finished() makes sure complete image data is received from n/w.

    @QByteArray      imageData   = reply->readAll();
    QImage          *image      = new QImage();
    
    image->loadFromData(imageData);
    

    @
    reply->readAll() should be saved as first call clears the data after returning it.

    @ image->save("C:/POS/Images/" + imageName + ".png"@

    don't forget to specify image format (either in filename or as parameter)


Log in to reply