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. QNetworkAccessManager Download Image
Qt 6.11 is out! See what's new in the release blog

QNetworkAccessManager Download Image

Scheduled Pinned Locked Moved Solved General and Desktop
qnetworkaccessmqpixmap
5 Posts 2 Posters 2.8k Views
  • 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.
  • E Offline
    E Offline
    elveatles
    wrote on last edited by
    #1

    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.

    1 Reply Last reply
    0
    • jsulmJ Offline
      jsulmJ Offline
      jsulm
      Lifetime Qt Champion
      wrote on last edited by
      #2

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

      https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      0
      • E Offline
        E Offline
        elveatles
        wrote on last edited by
        #3

        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.

        1 Reply Last reply
        0
        • jsulmJ Offline
          jsulmJ Offline
          jsulm
          Lifetime Qt Champion
          wrote on last edited by
          #4

          What is the content of thumbnailData after reading the reply?

          https://forum.qt.io/topic/113070/qt-code-of-conduct

          1 Reply Last reply
          0
          • E Offline
            E Offline
            elveatles
            wrote on last edited by
            #5

            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.

            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