Setting the value of one cell in QTableWidget fills everything.
Phil Thompson
phil at riverbankcomputing.com
Fri Mar 28 11:22:06 EDT 2008
On Friday 28 March 2008, Constantly Distracted wrote:
> I've just started PyQt programming and I've run into this little
> problem. When I set the text of one cell in my table, all the other
> cells fill with that value.
...because you have only created a single QTableWidgetItem instance, rather
than one for each cell.
> All I wanted to do was run a loop, printing in each cell the row and
> column number.
> Here's the code.
>
> ------------------------------
>
> from PyQt4 import QtGui,QtCore
> import sys
>
> class mywindow(QtGui.QWidget):
> def __init__(self,parent=None):
> QtGui.QWidget.__init__(self,parent)
> self.resize(700,700)
> self.mytable=QtGui.QTableWidget(7,6,self)
> self.mytable.resize(700,700)
>
> def filltable(self):
> items=QtGui.QTableWidgetItem()
> for column in range(self.mytable.columnCount()):
> for row in range(self.mytable.rowCount()):
> items.setText("row: " + str(row) + " column: " +
> str(column))
> self.mytable.setItem(row,column,items)
>
>
> app=QtGui.QApplication(sys.argv)
> mainwin=mywindow()
> mainwin.filltable()
> qb.show()
> app.exec_()
Move the creation of the QTableWidgetItem() to the inner loop (and call
it "item" rather than "items").
Phil
More information about the Python-list
mailing list