[PyQt] problem adding data to a QTableWidget

John Vines (CISD/HPCD) jvines at arl.army.mil
Mon Mar 3 20:11:02 GMT 2008


All,  I'm having some problems adding data to the second column of my 
table widget.  I do not see the problem, any help would be appreciated.

Thanks.
John

My class is derived from a QTableWidget.  I pass in a list of 2 item 
tuples, ("name","value").  I am able to populate the first column 
without issue, but the second column is not getting set???

Here is my code:
#!/bin/env python

import sys
from PyQt4 import QtGui, QtCore

class Table(QtGui.QTableWidget):
    def __init__(self,  parent=None,  *args,  **kwds):
        QtGui.QTableWidget.__init__(self, parent)
        self.library_values = kwds['data']
        self.BuildTable(self.library_values)
        
    def AddToTable(self, values):
        for k,  v in enumerate(values):
            self.AddItem(k,  v)
            
    def AddItem(self,  row,  data):
        for column, value in enumerate(data):
            item = QtGui.QTableWidgetItem( value )
            self.setItem(row, column,  item)

    def BuildTable(self,  values ):
        self.setSortingEnabled(False)
        headers = ['Variable',  'Value']
        self.setRowCount( len(values) )
        self.setColumnCount( len(headers) )
        self.setHorizontalHeaderLabels(headers)
        self.AddToTable(values)
        self.resizeColumnsToContents()
        
if __name__=="__main__":
    App = QtGui.QApplication([])
    d = [('a', 1), ('b', 2), ('c', 3),  \
                ('d', 4), ('e', 5), ('f', 6)]
    win = Table(data = d)
    win.show()
    sys.exit(App.exec_())


More information about the PyQt mailing list