[PyQt] problem adding data to a QTableWidget

Mark Summerfield mark at qtrac.eu
Tue Mar 4 07:18:19 GMT 2008


On 2008-03-03, John Vines (CISD/HPCD) wrote:
> #!/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 )

	      item = QtGui.QTableWidgetItem(str(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_())



-- 
Mark Summerfield, Qtrac Ltd., www.qtrac.eu




More information about the PyQt mailing list