Issue
I want to build a wrapper for my API using QNetworkAccessManager
and the asynchronous fetching with the get()
method. I create a subclass of QObject
, pass the parent class into __init__
and call super()
and the signal this_should_emit
never gets received.
# main.py
from PySide6.QtWidgets import QMainWindow
from api.initialize.version import GetVersion
from ui_build.main import Ui_MainWindow
class MainWindow(QMainWindow):
# Window Setup
def __init__(self):
super(MainWindow, self).__init__()
self.ui = Ui_MainWindow()
self.ui.setupUi(self)
get_version = GetVersion(parent=self)
get_version.this_should_emit.connect(self.verified_connection)
def verified_connection(self):
print("verified")
# version.py
import json
from PySide6.QtCore import QUrl, QObject, QEventLoop, Signal
from PySide6.QtNetwork import QNetworkRequest, QNetworkAccessManager
class GetVersion(QObject):
this_should_emit = Signal()
def __init__(self, parent=None):
super(GetVersion, self).__init__(parent)
print(parent)
loop = QEventLoop()
manager = QNetworkAccessManager()
manager.finished.connect(self.get_reply)
reply = manager.get(QNetworkRequest(QUrl("http://localhost:5000/v1/test")))
reply.finished.connect(loop.exit)
loop.exec()
self.this_should_emit.emit()
def __del__(self):
print("deleted")
def get_reply(self, n):
print('got reply')
temp = bytes(n.readAll()).decode("utf8")
temp = json.loads(temp)
print(temp)
self.this_should_emit.emit()
Solution
The answer to my solution wasn't trying to do any trickery with another QEventLoop, but rather making sure the QNetworkAccessManager had access to the QDialog as a parent.
Answered By - hlafaille
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.