pyqt - Multiple requests using QNetworkAccessManager qt -
i send multiple post requests using single qnetworkaccessmanager
, works once. second , further requests got empty reply. know can use multiple instances of manager, read, it's architectural flaw. have found this: using qnetworkaccessmanager multiple times, not understand eventual solution. here's code:
class networker(qobject): gotresponse = pyqtsignal(str) def proc(self, reply): s = bytes(reply.readall()).decode('utf-8') self.gotresponse.emit(s) def __init__(self, parent=none): super(networker, self).__init__(parent) self.manager = qnetworkaccessmanager() def send(self): req = qnetworkrequest(qurl("http://127.0.0.1:8000/")) req.setheader(qnetworkrequest.contenttypeheader, "text/json;charset=utf-8") data = qbytearray('{"id":1}') self.manager.finished.connect(self.proc) self.manager.post(req, data)
main (imports excluded):
def main(): app = qtwidgets.qapplication(sys.argv) n = networker(app) w = qtwidgets.qwidget() w.setwindowtitle("main") layout = qboxlayout(qboxlayout.lefttoright, w) l1 = qtextedit("result", w) l1.resize(300, 600) n.gotresponse.connect(l1.settext) layout.addwidget(l1) w.show() l2 = qtextedit("idle", w) l2.resize(250, 600) layout.addwidget(l2) w.resize(500, 600) btn = qpushbutton("send", w) layout.addwidget(btn) btn.clicked.connect(n.send) w.show() sys.exit(app.exec_()) if __name__ == '__main__': main()
Comments
Post a Comment