python logo

QT4 Table


Python hosting: Host, run, and code Python in the cloud!

We can show a table using the QTableWidget, part of the PyQt module.  We set the title, row count, column count and add the data.

Related course:

Qt4 Table example


An example below:

from PyQt4.QtGui import *
from PyQt4.QtCore import *
import sys

def main():
app = QApplication(sys.argv)
table = QTableWidget()
tableItem = QTableWidgetItem()

# initiate table
table.setWindowTitle("QTableWidget Example @pythonspot.com")
table.resize(400, 250)
table.setRowCount(4)
table.setColumnCount(2)

# set data
table.setItem(0,0, QTableWidgetItem("Item (1,1)"))
table.setItem(0,1, QTableWidgetItem("Item (1,2)"))
table.setItem(1,0, QTableWidgetItem("Item (2,1)"))
table.setItem(1,1, QTableWidgetItem("Item (2,2)"))
table.setItem(2,0, QTableWidgetItem("Item (3,1)"))
table.setItem(2,1, QTableWidgetItem("Item (3,2)"))
table.setItem(3,0, QTableWidgetItem("Item (4,1)"))
table.setItem(3,1, QTableWidgetItem("Item (4,2)"))

# show table
table.show()
return app.exec_()

if __name__ == '__main__':
main()

Result:

PyQT Table PyQt Table

QTableWidget labels


You can set the header using the setHorizontalHeaderLabels() function. The same applies for vertical labels. A qt4 demonstration below:

from PyQt4.QtGui import *
from PyQt4.QtCore import *
import sys

def main():
app = QApplication(sys.argv)
table = QTableWidget()
tableItem = QTableWidgetItem()

# initiate table
table.setWindowTitle("QTableWidget Example @pythonspot.com")
table.resize(400, 250)
table.setRowCount(4)
table.setColumnCount(2)

# set label
table.setHorizontalHeaderLabels(QString("H1;H2;").split(";"))
table.setVerticalHeaderLabels(QString("V1;V2;V3;V4").split(";"))

# set data
table.setItem(0,0, QTableWidgetItem("Item (1,1)"))
table.setItem(0,1, QTableWidgetItem("Item (1,2)"))
table.setItem(1,0, QTableWidgetItem("Item (2,1)"))
table.setItem(1,1, QTableWidgetItem("Item (2,2)"))
table.setItem(2,0, QTableWidgetItem("Item (3,1)"))
table.setItem(2,1, QTableWidgetItem("Item (3,2)"))
table.setItem(3,0, QTableWidgetItem("Item (4,1)"))
table.setItem(3,1, QTableWidgetItem("Item (4,2)"))

# show table
table.show()
return app.exec_()

if __name__ == '__main__':
main()

Result:

PyQT Table PyQT Table

Note: These days you can use pyqt5 to create a pyqt table.

QTableWidget click events


We can detect cell clicks using this procedure, first add a function:

# on click function
table.cellClicked.connect(cellClick)

Then define the function:

def cellClick(row,col):
print "Click on " + str(row) + " " + str(col)

The Python programming language starts counting with 0, so when you press on (1,1) you will see (0,0). Full code to detect table clicks:

from PyQt4.QtGui import *
from PyQt4.QtCore import *
import sys

def cellClick(row,col):
print "Click on " + str(row) + " " + str(col)

def main():
app = QApplication(sys.argv)
table = QTableWidget()
tableItem = QTableWidgetItem()

# initiate table
table.setWindowTitle("QTableWidget Example @pythonspot.com")
table.resize(400, 250)
table.setRowCount(4)
table.setColumnCount(2)

# set label
table.setHorizontalHeaderLabels(QString("H1;H2;").split(";"))
table.setVerticalHeaderLabels(QString("V1;V2;V3;V4").split(";"))

# set data
table.setItem(0,0, QTableWidgetItem("Item (1,1)"))
table.setItem(0,1, QTableWidgetItem("Item (1,2)"))
table.setItem(1,0, QTableWidgetItem("Item (2,1)"))
table.setItem(1,1, QTableWidgetItem("Item (2,2)"))
table.setItem(2,0, QTableWidgetItem("Item (3,1)"))
table.setItem(2,1, QTableWidgetItem("Item (3,2)"))
table.setItem(3,0, QTableWidgetItem("Item (4,1)"))
table.setItem(3,1, QTableWidgetItem("Item (4,2)"))

# on click function
table.cellClicked.connect(cellClick)

# show table
table.show()
return app.exec_()

if __name__ == '__main__':
main()

If you want to show the cell/row numbers in a non-programmer way use this instead:

def cellClick(row,col):
print "Click on " + str(row+1) + " " + str(col+1)

Tooltip text


We can set tooltip (mouse over) text using the method. If you set tooltips on non-existing columns you will get an error.

from PyQt4.QtGui import *
from PyQt4.QtCore import *
import sys

def main():
app = QApplication(sys.argv)
table = QTableWidget()
tableItem = QTableWidgetItem()

# initiate table
table.setWindowTitle("QTableWidget Example @pythonspot.com")
table.resize(400, 250)
table.setRowCount(4)
table.setColumnCount(2)

# set label
table.setHorizontalHeaderLabels(QString("H1;H2;").split(";"))
table.setVerticalHeaderLabels(QString("V1;V2;V3;V4").split(";"))

# set data
table.setItem(0,0, QTableWidgetItem("Item (1,1)"))
table.setItem(0,1, QTableWidgetItem("Item (1,2)"))
table.setItem(1,0, QTableWidgetItem("Item (2,1)"))
table.setItem(1,1, QTableWidgetItem("Item (2,2)"))
table.setItem(2,0, QTableWidgetItem("Item (3,1)"))
table.setItem(2,1, QTableWidgetItem("Item (3,2)"))
table.setItem(3,0, QTableWidgetItem("Item (4,1)"))
table.setItem(3,1, QTableWidgetItem("Item (4,2)"))

# tooltip text
table.horizontalHeaderItem(0).setToolTip("Column 1 ")
table.horizontalHeaderItem(1).setToolTip("Column 2 ")

# show table
table.show()
return app.exec_()

if __name__ == '__main__':
main()

Result:

PyQT Table tooltips PyQT Table tooltips

Download PyQT Code (Bulk Collection)

BackNext





Leave a Reply:




Smgk Wed, 22 Jul 2015

How can I make the Table not be in the corner or the window? And how can I put 2 buttons in a single entry of a table?

Frank Wed, 22 Jul 2015

Hi, I recommend QTCreator for this. It will generate the code for you

Chen Sun, 23 Aug 2015

hello!I want to read the items selected,how to define the signal and slot?Thank you

Frank Sun, 23 Aug 2015

Use itemSelectionChanged.connect() to receive the selections. I have created an example of selection with a QTableWidget below. I've changed the code slightly to be object oriented, but the other method should work too.

from PyQt4.QtGui import *
from PyQt4.QtCore import *
import sys

class QTable(QTableWidget):
@pyqtSlot()
def on_click(self):
print("\n")
for currentQTableWidgetItem in self.selectedItems():
print currentQTableWidgetItem.row(), currentQTableWidgetItem.column(), currentQTableWidgetItem.text()

def __init__(self, parentQWidget = None):
super(QTable, self).__init__(parentQWidget)

# initiate table
self.setWindowTitle("QTableWidget Example @pythonspot.com")
self.resize(400, 250)
self.setRowCount(4)
self.setColumnCount(2)

# set data
self.setItem(0,0, QTableWidgetItem("Item (1,1)"))
self.setItem(0,1, QTableWidgetItem("Item (1,2)"))
self.setItem(1,0, QTableWidgetItem("Item (2,1)"))
self.setItem(1,1, QTableWidgetItem("Item (2,2)"))
self.setItem(2,0, QTableWidgetItem("Item (3,1)"))
self.setItem(2,1, QTableWidgetItem("Item (3,2)"))
self.setItem(3,0, QTableWidgetItem("Item (4,1)"))
self.setItem(3,1, QTableWidgetItem("Item (4,2)"))

# table selection change
self.itemSelectionChanged.connect(self.on_click)


if __name__ == '__main__':
app = QApplication(sys.argv)
mytable = QTable()
mytable.show()
app.exec_()
Hussain Dholkawala Thu, 17 Sep 2015

I am trying track double click on a table cell and get value of the cell? I am using python 2.7 and pyQt how do i do that?

Frank Thu, 17 Sep 2015

Change to self.doubleClicked.connect. Here is a working example:

from PyQt4.QtGui import *
from PyQt4.QtCore import *
import sys

class QTable(QTableWidget):
@pyqtSlot()
def on_click(self):
print("\n")
for currentQTableWidgetItem in self.selectedItems():
print currentQTableWidgetItem.row(), currentQTableWidgetItem.column(), currentQTableWidgetItem.text()

def __init__(self, parentQWidget = None):
super(QTable, self).__init__(parentQWidget)

# initiate table
self.setWindowTitle("QTableWidget Example @pythonspot.com")
self.resize(400, 250)
self.setRowCount(4)
self.setColumnCount(2)

# set data
self.setItem(0,0, QTableWidgetItem("Item (1,1)"))
self.setItem(0,1, QTableWidgetItem("Item (1,2)"))
self.setItem(1,0, QTableWidgetItem("Item (2,1)"))
self.setItem(1,1, QTableWidgetItem("Item (2,2)"))
self.setItem(2,0, QTableWidgetItem("Item (3,1)"))
self.setItem(2,1, QTableWidgetItem("Item (3,2)"))
self.setItem(3,0, QTableWidgetItem("Item (4,1)"))
self.setItem(3,1, QTableWidgetItem("Item (4,2)"))

# table selection change
self.doubleClicked.connect(self.on_click)


if __name__ == '__main__':
app = QApplication(sys.argv)
mytable = QTable()
mytable.show()
app.exec_()
Iman Shadmehri Tue, 03 Nov 2015

very useful
Thanks

Gorzo Sun, 10 Jan 2016

How I can make variable number of column entries in table. I want user to have "add column" choice that will add a new column and allow changing number of entries. How I make this?

Michel Wed, 13 Jan 2016

Hi, how can I make a table that can collapse?

Frank Thu, 28 Jan 2016

People recommend a QTreeView for this, haven't tried it myself. http://stackoverflow.com/qu...

Frank Sat, 30 Jan 2016

Create an onbutton event. In the event of a button click, resize the table and call the show table method.

Ayoub Mon, 30 May 2016

Thank so mush ....
The PyQt Tutorials are limited out there... comparing it the monster of Oracl(Javafx) ...
You saved for me a 2+ hours of searching on web ... thank you ... Keep up the good work.

Eli Mon, 20 Jun 2016

Hallo,

thank you for this helpful post. I am trying to get the values entered by the user and put them in a list but
I get this error: NoneType' object has no attribute 'text
Any idea?
thanx!

This is my code:
[python]
mytable = self.dlg.tableWidget
lista = []
listb = []
# i is always in range 4 in my code
for i in range(4):
# j is always the length of the unique values list of a field of a qgis layer,
# selected by the user on a previous step
for j in range(un_values_len):
a_item = mytable.item(i, j)
a_name = str(a_item.text())
lista.append(a_item)
listb.append(a_name)

print lista, listb
[/python]