'QTableWidget doesn't show values

I am trying to insert a data set into a QTableWidget using PyQt. However, when I use tableWidget.setItem() my IDE crashes.

        usuarios = db.child("operacoes").get()
        line = 0
        for usuario in usuarios.each():
            dados = list(usuario.val().values())
            self.ui.tableWidget.setItem(line, 0, QTableWidgetItem(dados[0]))
            self.ui.tableWidget.setItem(line, 1, QTableWidgetItem(dados[1]))
            self.ui.tableWidget.setItem(line, 2, QTableWidgetItem(dados[2]))
            self.ui.tableWidget.setItem(line, 3, QTableWidgetItem(dados[3]))
            self.ui.tableWidget.setItem(line, 4, QTableWidgetItem(str(dados[4])))
            self.ui.tableWidget.setItem(line, 5, QTableWidgetItem(str(dados[5])))
            self.ui.tableWidget.setItem(line, 6, QTableWidgetItem(str(dados[6])))
            line = line + 1

The data is retrieved from Firebase and stored in a vector. I'm trying to use the same loop that I use to retrieve the data from firebase to fill the table. However, when I execute this code my QTableWidget is blank.

My dados vector has the following values:

dados[0] = "01/01/2000"
dados[1] = "JPY"
dados[2] = "teste"
dados[3] = "BRL"
dados[4] = 1.40
dados[5] = 14.00
dados[6] = 100

My table is designed like this using QtDesigner:

enter image description here

How can I solve this?



Solution 1:[1]

Following the docs of QTableWidget, before adding QTableWidgetItems into cells you must declare the size of the QTableWidget (rows and columns). This can be done at the creation of the table object (in the constructor), or changing the size of the table before inserting items. In your case, you probably have created a table with a size of 1 row x 7 columns, so you must resize the table before inserting the items. This can be achieved adding

usuarios = db.child("operacoes").get()
line = 0
self.ui.tableWidget.setRowCount(len(list(usuarios.val().values())))
for usuario in usuarios.each():
    dados = list(usuario.val().values())
    self.ui.tableWidget.setItem(line, 0, QTableWidgetItem(dados[0]))
    ....

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 AndrewQ