Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. Qt for Python
  4. QNetworkAccessManager finished.connect never called
Forum Updated to NodeBB v4.3 + New Features

QNetworkAccessManager finished.connect never called

Scheduled Pinned Locked Moved Solved Qt for Python
3 Posts 2 Posters 596 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.
  • T Offline
    T Offline
    Tyskie
    wrote on last edited by Tyskie
    #1

    Hello everyone,
    I have a weird situation where my QNetworkAccessManager's finished slot is never triggering the callback...it drives me crazy :D
    Maybe one of you see what I am missing?

    here is the code:

    import sys
    from PyQt5.QtWidgets import QApplication
    from PyQt5.QtCore import QUrl
    from PyQt5.QtWidgets import QWidget
    from PyQt5.QtNetwork import QNetworkAccessManager, QNetworkRequest
    
    
    class ImageGetter:
        def __init__(self, callback, url, params):
            self.url = url
            self.callback = callback
            self.params = params
    
        def from_url(self):
            manager = QNetworkAccessManager()
            request = QNetworkRequest(QUrl(self.url))
            manager.finished.connect(self.dupa)
            manager.get(request)
            print('till here everything is fine')
    
        def dupa(self, reply):
            print('this is never called')
            logging.error(args)
            logging.error(kwargs)
    
    
    def get_image_from_url(callback, url, **params):
        image_getter = ImageGetter(callback, url, params)
        image_getter.from_url()
    
    
    def callback():
        raise NotImplementedError('reserved for future usage')
    
    
    if __name__ == '__main__':
    
        app = QApplication(sys.argv)
        w = QWidget()
        w.show()
        get_image_from_url(
            callback,
            'https://cdn.discordapp.com/embed/avatars/3.png',
            identifier='def'
        )
    
        sys.exit(app.exec_())
    

    the output:

    (.pyvenv) D:\Workspace\private\discord_overlay>python test.py
    till here everything is fine
    
    JonBJ 1 Reply Last reply
    0
    • T Tyskie

      Hello everyone,
      I have a weird situation where my QNetworkAccessManager's finished slot is never triggering the callback...it drives me crazy :D
      Maybe one of you see what I am missing?

      here is the code:

      import sys
      from PyQt5.QtWidgets import QApplication
      from PyQt5.QtCore import QUrl
      from PyQt5.QtWidgets import QWidget
      from PyQt5.QtNetwork import QNetworkAccessManager, QNetworkRequest
      
      
      class ImageGetter:
          def __init__(self, callback, url, params):
              self.url = url
              self.callback = callback
              self.params = params
      
          def from_url(self):
              manager = QNetworkAccessManager()
              request = QNetworkRequest(QUrl(self.url))
              manager.finished.connect(self.dupa)
              manager.get(request)
              print('till here everything is fine')
      
          def dupa(self, reply):
              print('this is never called')
              logging.error(args)
              logging.error(kwargs)
      
      
      def get_image_from_url(callback, url, **params):
          image_getter = ImageGetter(callback, url, params)
          image_getter.from_url()
      
      
      def callback():
          raise NotImplementedError('reserved for future usage')
      
      
      if __name__ == '__main__':
      
          app = QApplication(sys.argv)
          w = QWidget()
          w.show()
          get_image_from_url(
              callback,
              'https://cdn.discordapp.com/embed/avatars/3.png',
              identifier='def'
          )
      
          sys.exit(app.exec_())
      

      the output:

      (.pyvenv) D:\Workspace\private\discord_overlay>python test.py
      till here everything is fine
      
      JonBJ Offline
      JonBJ Offline
      JonB
      wrote on last edited by JonB
      #2

      @Tyskie
      Your manager = QNetworkAccessManager() goes out of scope (Python destroys it) at the end of from_url() method. That's before the finished arrives. Objects like this must persist. You probably want to make a member variable of ImageGetter? But then the same may be happening to your image_getter in get_image_from_url()? Anyway, that's what you need to look at.

      I don't understand why you have any Python global functions here. I would make everything, including slots, members of some class you instantiate.

      1 Reply Last reply
      3
      • T Offline
        T Offline
        Tyskie
        wrote on last edited by
        #3

        Thanks for you answer, I think I see now I will try to keep it alive then
        About the global functions they are in fact pretty deep in the code, but are attached to a class, that was just for simplicity.

        1 Reply Last reply
        1

        • Login

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