SQLite 和 tableWidget
SQLite and tableWidget
如何让QTableWidget中的Headers在调用loadData后也更新。
def loadData(self):
connection = sqlite3.connect('CUSP.db')
query = "SELECT * FROM ZAYAVITEL"
result = connection.execute(query)
self.tableWidget.setColumnCount(6)
for row_number, row_data in enumerate(result):
self.tableWidget.insertRow(row_number)
for column_number, data in enumerate(row_data):
self.tableWidget.setItem(row_number, column_number, QtWidgets.QTableWidgetItem(str(data)))
connection.close()
您可以使用 cursor.description
:
获取列的名称
def loadData(self):
self.tableWidget.setColumnCount(0)
self.tableWidget.setRowCount(0)
connection = sqlite3.connect("CUSP.db")
cursor = connection.execute("SELECT * FROM ZAYAVITEL")
#
names = [description[0] for description in cursor.description]
self.tableWidget.setColumnCount(len(names))
self.tableWidget.setHorizontalHeaderLabels(names)
for i, row_data in enumerate(cursor):
self.tableWidget.insertRow(i)
for j, value in enumerate(row_data):
it = QtWidgets.QTableWidgetItem()
it.setData(QtCore.Qt.DisplayRole, value)
self.tableWidget.setItem(i, j, it)
如何让QTableWidget中的Headers在调用loadData后也更新。
def loadData(self):
connection = sqlite3.connect('CUSP.db')
query = "SELECT * FROM ZAYAVITEL"
result = connection.execute(query)
self.tableWidget.setColumnCount(6)
for row_number, row_data in enumerate(result):
self.tableWidget.insertRow(row_number)
for column_number, data in enumerate(row_data):
self.tableWidget.setItem(row_number, column_number, QtWidgets.QTableWidgetItem(str(data)))
connection.close()
您可以使用 cursor.description
:
def loadData(self):
self.tableWidget.setColumnCount(0)
self.tableWidget.setRowCount(0)
connection = sqlite3.connect("CUSP.db")
cursor = connection.execute("SELECT * FROM ZAYAVITEL")
#
names = [description[0] for description in cursor.description]
self.tableWidget.setColumnCount(len(names))
self.tableWidget.setHorizontalHeaderLabels(names)
for i, row_data in enumerate(cursor):
self.tableWidget.insertRow(i)
for j, value in enumerate(row_data):
it = QtWidgets.QTableWidgetItem()
it.setData(QtCore.Qt.DisplayRole, value)
self.tableWidget.setItem(i, j, it)