I have a Client(C++) to Server (Python) Project.
The client sent a Message to the server, and each Message contains three attributes.
The Gui should show all the attributes over the screen. When the client sent the first message, everything works. But when he sent the next message I need to close the window, and when I do that it opens a new window and shows the following message, if I don't do that he doesn't show the new messages that received.
I suspect that app.exec_()
stop the program from getting a new message like in this case, but I'm not sure.
my question is :
Is this really the case? And if so, where should I put the Thread?
I never used
QThread
before, and I saw that we are doing Override forRun()
and in theRun()
method we put in all the logic, what logic I should put into my particular problem. you can find the full project here
Relevant code:
def recive(self):
global income
global table2
msg = Payload(0, 0, 0)
while (True):
print('waiting for a connection..')
conn, addr = self.My_socket.accept()
print("connection has been established | " + repr(addr))
logger.info("connection has been established | " + repr(addr))
while conn:
myThread = MyThread()
buff = conn.recv(sizeof(msg))
print("recv %d bytes" % sizeof(msg))
payload_in = Payload.from_buffer_copy(buff)
print(f"Received id={payload_in.id}, counter={payload_in.counter}, opcode={payload_in.opcode}")
payload_out = payload_in
self.opcode = payload_in.opcode
if self.opcode == 1:
self.export()
elif self.opcode == 2:
payload_out.counter += 1
else:
logger.info("Unexpected opcode %d" % self.opcode)
nsent = conn.send(payload_out)
print("send %d bytes" % nsent)
print("send id=%d, counter=%d, opcode=%d" % (payload_out.id,
payload_out.counter,
payload_out.opcode))
self.setNewTable(payload_in, payload_out)
print("Closing connection to client")
print("----------------------------")
sys.exit()
def setNewTable(self,payload_in, payload_out):
self.insertValues(table1, payload_in)
self.insertValues(table2, payload_out)
ex.show()
app.exec_()
def insertValues(self,table, payload):
table.setItem(0, 1, QTableWidgetItem(str(payload.id)))
table.setItem(1, 1, QTableWidgetItem(str(payload.counter)))
table.setItem(2, 1, QTableWidgetItem(str(payload.opcode)))