Skip to content Skip to sidebar Skip to footer

Adding Data To Qtablewidget Using Pyqt4 In Python

I want to add my data to a table using pyqt in python. I found that I should use setItem() function to add data to a QTableWidget and give it the row and column number and a QTable

Solution 1:

What you are looking for are the setRowCount() and setColumnCount() methods. Call these on the QTableWidget to specify the number of rows/columns. E.g.

...
self.table = QtGui.QTableWidget()
self.table.setRowCount(5)
self.table.setColumnCount(5)
layout.addWidget(self.led, 0, 0)
layout.addWidget(self.table, 1, 0)
self.table.setItem(1, 0, QtGui.QTableWidgetItem(self.led.text()))
...

This code will make a 5x5 table and display "Sample" in the second row (with index 1) and first column (with index 0).

Without calling these two methods, QTableWidget would not know how large the table is, so setting the item at position (1, 0) would not make sense.

In case you are unaware of it, the Qt Documentation is detailed and contains many examples (which can easily be converted to Python). The "Detailed Description" sections are especially helpful. If you want more information about QTableWidget, go here: http://qt-project.org/doc/qt-4.8/qtablewidget.html#details

Post a Comment for "Adding Data To Qtablewidget Using Pyqt4 In Python"