Solved QNetworkAccessManager finished.connect never called
-
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
-
@Tyskie
Yourmanager = QNetworkAccessManager()
goes out of scope (Python destroys it) at the end offrom_url()
method. That's before thefinished
arrives. Objects like this must persist. You probably want to make a member variable ofImageGetter
? But then the same may be happening to yourimage_getter
inget_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.
-
@Tyskie
Yourmanager = QNetworkAccessManager()
goes out of scope (Python destroys it) at the end offrom_url()
method. That's before thefinished
arrives. Objects like this must persist. You probably want to make a member variable ofImageGetter
? But then the same may be happening to yourimage_getter
inget_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.
-
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.