QT4 Table


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.

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 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

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)


13 thoughts on “QT4 Table

  1. Eli - June 20, 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:

    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
  2. Ayoub - May 30, 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.