QTableWidgetItem is getting itmes method not working

247 views Asked by At

I am not able to populate this table, I do not knoe what I'm doing wrong, I have been reading some posts, and seems like everything is correct...however, nothing is appearing in my table.

Here is my code:

The totalProcess list is like that totalProcess = [ [list1],[list2],[list3]...]

def updateTable(self,totalProcess):

   for x in xrange(10):
       for i in xrange(len(totalProcess[x])):

           item = QtGui.QTableWidgetItem(totalProcess[x][i])
           self.ui.tableWidgetResults.setItem(x,i,item)

Any help will be appreciated!!

2

There are 2 answers

6
Ramchandra Apte On

Have QTableWidget's rowCount and columnCount attributes been set? QTableWidget will only display items within them. Otherwise the code is correct.

PS: There is no need to loop through the indices as in some other languages; you can use for x in list to iterate through the element of the list (x will be the element of the list).

2
Aleksandar On

I mean, let's say that we know the number os columns, 10, from the beginning to the end, but we dont know the number of rows, like in an acquisition system, we just keep on adding new rows until the acquisition is finished. Would it be possible?

Yes, it is possible, and it's very simple. For example you could do that like this:

 def AddRowToTable(self, list):                      # list is one of those from your totalProcess = [ [list1],[list2],[list3]...]
    row = self.ui.tableWidgetResults.rowCount()      # get current number of rows
    self.ui.tableWidgetResults.insertRow(row)        # insert new row
    for col in range(0, len(list)):                  # add items into row
        item = QtGui.QTableWidgetItem(QtCore.QString(unicode(list[col])))
        self.ui.tableWidgetResults.setItem(row, col, item)

now seems like it is working but maybe im getting some empty cells in between

that's why I've added list[col] into this QtCore.QString(unicode(list[col])) - to ensure it is valid value type for QtGui.QTableWidgetItem input parametar.

Now you can simply iterate through totalProcess and for every list in it call AddRowToTable. PS Don't forget to setColumnCount before everythng.