1
votes

How to make Headers in the QTableWidget also updated after calling the 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()
1
Welcome to stackoverflow! Can you please provide more information on your problem? What is that you are trying to achieve? What did you try? What problems have you found so far? Feel free to add these informations by editing your question. - bracco23

1 Answers

1
votes

You can get the name of the columns using the cursor.description:

def loadData(self):
    self.tableWidget.setColumnCount(0)
    self.tableWidget.setRowCount(0)

    connection = sqlite3.connect("CUSP.db")
    cursor = connection.execute("SELECT * FROM ZAYAVITEL")
    # https://stackoverflow.com/a/7831685/6622587
    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)