Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. [solved] get and save Image from URL is not working

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

Scheduled Pinned Locked Moved General and Desktop
3 Posts 2 Posters 1.3k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • A Offline
    A Offline
    Abin
    wrote on last edited by
    #1

    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...

    1 Reply Last reply
    0
    • dheerendraD Offline
      dheerendraD Offline
      dheerendra
      Qt Champions 2022
      wrote on last edited by
      #2

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

      Dheerendra
      @Community Service
      Certified Qt Specialist
      http://www.pthinks.com

      1 Reply Last reply
      0
      • A Offline
        A Offline
        Abin
        wrote on last edited by
        #3

        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)

        1 Reply Last reply
        0

        • Login

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • Users
        • Groups
        • Search
        • Get Qt Extensions
        • Unsolved