QNetworkAccessManager Download Image



  • Hi, I'm using PySide and I'm having trouble figuring out why I can't download an image properly using QNetworkAccessManager.
    Here's what I'm doing:

    manager = QNetworkAccessManager()
    
    def downloadThumbnail(self):
        thumbnail = 'https://.../some-thumbnail.jpg'
        request = QNetworkRequest(QUrl(thumbnail)) 
        reply = manager.get(request)
        reply.finished.connect(self.onThumbnailLoaded)
    
    @Slot()
    def onThumbnailLoaded(self):
        if reply.error() == QNetworkReply.NoError:
            thumbnailData = reply.readAll()
            pixmap = QPixmap()
            pixmap.loadFromData(thumbnailData)
            self.someLabel.setPixmap(pixmap)
    

    There is no reply error. reply.readAll() does not have an error. pixmap.loadFromData returns False, but I don't know why.


  • Moderators

    I would check what reply.readAll() returns. You can even store it in a file and check that file.



  • I updated the code to be:

    thumbnailData = reply.readAll()
    tempFile = QFile(r'C:\temp.jpg')
    tempFile.open(QIODevice.WriteOnly)
    tempFile.write(thumbnailData)
    tempFile.close()
    

    When I open the file, it says: "Windows Photo Viewer can't open this picture because the file appears to be damaged, corrupted, or is too large."

    But if I open the url in a browser I can see the image. If I do thumbnailData.count(), I get 304, so there is something there. I don't know what I'm doing wrong.


  • Moderators

    What is the content of thumbnailData after reading the reply?



  • Printing out thumbnailData.data(), I get:

    302 Found

    The resource was found at https://...

    So I realize my problem now. The thumbnail url is redirecting me to another url which is what I actually want to use to download for the QPixmap.

    Thanks for your help everyone.


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.