0
votes

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?

1
Please include the error you get (if any). Check if 'dados' contains valid values. - AndrewQ
@AndrewQ I got no error messages and dados contain valid values (i've printed on console). - user2348911
@AndrewQ I've edited the original question with the dados values. - user2348911
Did you create the QTableWidget with the right number of rows and columns? What you mean with "my IDE crashes"? Your program or i.e. PyCharm? - AndrewQ
@AndrewQ The program stops running and I have to close it in the IDE. - user2348911

1 Answers

0
votes

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]))
    ....