"select * from {tableName} limit {offset},{pageSize}".format( **queryParam)) header = [item[0] for item in cursor.description] datas = cursor.fetchall() table.setColumnCount(len(header)) # 注意必须在初始化行列之后进行,否则,没有效果 table.setHorizontalHeaderLabels(header) table.setRowCount(len(datas)) for i, row in enumerate(datas): for j, data in enumerate(row, start=0): item = QTableWidgetItem(str(data)) table.setItem(i, j, item) if __name__ == '__main__': import os os.chdir("../..") print(os.getcwd()) app = QApplication(sys.argv) # 创建窗口 example = DbTableWidget() example.resize(1000, 800) # 显示窗口 example.show() example.loadTable() sys.exit(app.exec_())