需要先给Qtable.Widget设置好行数。
self.tableWidget.setRowCount(row)
示例片段
cursor.execute(sql, params)
rows = cursor.fetchall()
row = cursor.rowcount # 行数
if rows:
col = len(rows[0]) #列数
self.tableWidget.setRowCount(row)
for i in range(row):
for j in range(col):
temp_data = rows[i][j] # 临时记录,不能直接插入表格
print(temp_data)
data = QTableWidgetItem(str(temp_data)) # 转换后可插入表格
self.tableWidget.setItem(i, j, data)
else:
self.tableWidget.setRowCount(0)