QTcpsocket/QTcpserver传输,如何使用QdataStream传输一个表数据??
有思路:在每一行写数据流,然后调用write(),但是怎么再写呢?
存在问题:数据乱码,第一行数据重复写入。
从fh文件中提取保存的数据流数据,写self.block进行TCP传输(数据以表格每一行的格式保存,读出行数和列表数据)->不能正常显示求例子或想法,用 PYQT5 编写
服务器代码:
self.block = QByteArray()
fh = QFile(name)
fh.open(QIODevice.ReadOnly)
tableData = QDataStream(fh) # Get Data from File
tableData.setVersion(QDataStream.Qt_5_6)
stream = QDataStream(self.block, QIODevice.WriteOnly) # Input data to socket
stream.setVersion(QDataStream.Qt_5_6)
self.clientConnection = self.server.nextPendingConnection()
while not tableData.atEnd():
num,table=0,''
num = tableData.readUInt16()
table = tableData.readQStringList()
stream.writeUInt16(0)
stream.writeUInt16(num)
stream.writeQStringList(table)
stream.device().seek(0)
stream.writeUInt16(self.block.size() - SIZEOF_INT16) #
self.clientConnection.write(self.block)
self.clientConnection.flush()
客户端.py
def readMessage(self):
try:
print('readmessage')
stream = QDataStream(self.socket)
stream.setVersion(QDataStream.Qt_5_6)
while not stream.atEnd():
if self.blockSize == 0:
if self.socket.bytesAvailable()<SIZEOF_UINT16:
return
self.blockSize = stream.readUInt16()
if self.socket.bytesAvailable()<self.blockSize:
return
num=stream.readUInt16()
message = stream.readQStringList() #?????????
print(num,message)
if num<1:
self.tableWidget.setRowCount(1) # First row Data
for a in range(0, 11):
# print(a)
newItem = QTableWidgetItem(message[a])
self.tableWidget.setItem(0, a, newItem)
else:
self.tableWidget.insertRow(0)
for a in range(0, 11):
newItem = QTableWidgetItem(message[a])
self.tableWidget.setItem(0, a, newItem)
print('closee')
self.socket.disconnect()
self.socket.close()
except:
self.socket.close()
输入数据:
错误输出数据: