pythonpython-3.xormpyqt5pony

Why pyqt tablewigdet is only displaying row number and no data?


I wonder if the problem is from pony.orm or from my python code?

@db_session
def load_data(self):
    result = db.execute("""SELECT * FROM Product""")
    self.ui.tableWidget.setRowCount(0)
    for row_number, row_data in enumerate(result):
        self.ui.tableWidget.insertRow(row_number)
        for column_number, data in enumerate(row_data):
            self.ui.tableWidget.setItem(row_number, column_number, QTableWidgetItem(str(data)))

Solution

  • The columnCount property is 0 by default, so it must be set to the actual column count in order to correctly set and show the items.

    @db_session
    def load_data(self):
        result = db.execute("""SELECT * FROM Product""")
        self.ui.tableWidget.setRowCount(0)
        for row_number, row_data in enumerate(result):
            self.ui.tableWidget.insertRow(row_number)
            if len(row_data) > self.ui.tableWidget.columnCount():
                self.ui.tableWidget.setColumnCount(len(row_data))
            for column_number, data in enumerate(row_data):
                self.ui.tableWidget.setItem(row_number, column_number, QTableWidgetItem(str(data)))