PyQT Add and Show a Table: A Beginner Guide – PyQT Tutorial

By | December 12, 2019

Table widget is widely used in PyQT, which is a good way to display data. How to add and show a table widget in pyqt? In this tutorial, we will introduce pyqt beginners how to do.

Preliminary

To add and show a table in pyqt script, there are some basic knowlege you should kown:

Look at image below:

pyqt add table tutorials and examples

From this image, we can find:

1.A table contains two part: a table header and a table body.

2.A table body contains many cells, the postion of each cell is (row, col). The value of row and col start with (0, 0).

Understand the structure of table, we can add it easily.

How to create a table in pyqt?

We can use QTableWidget() to create a table. Here is an example:

from PyQt5.QtWidgets import QTableWidget,QTableWidgetItem

table = QTableWidget(self)

After creating a table object, we should add a table header for it.

How to add a table header?

Here is an example:

table.setHorizontalHeaderLabels(['id','Name','Age','Sex','Address'])

In this tutorial, we will add 5 text in table header.

Then, we should tell table how many cells will be saved, which means we should set the row and column count of table.

How to set row and column count of table

We can do it like this:

table.setColumnCount(5)
table.setRowCount(10)

In this tutorial, we set row count is 10 and column count is 5, which means this table will save 50 cells. If you have a 11* 5 data, the last row will not be saved in this table, which means the last row is invisible.

How to add data to table

After setting the row and column count of a table, we can add some data to it. In pyqt, we can add data cell by cell, which means we can add a cell by its position from (0, 0) to (row-1, col – 1).

        table_data = []
        item_1 = ['001', 'John', 30, 'Male', 'Street No 2']
        item_2 = ['002', 'Lily', 32, 'Female', 'Street No 1']
        item_3 = ['003', 'Kate', 20, 'Male', 'Street No 3']
        item_4 = ['004', 'Tom', 22, 'Male', 'Street No 4']
        table_data.append(item_1)
        table_data.append(item_2)
        table_data.append(item_3)
        table_data.append(item_4)

Here we have created 4 * 5 data, we will add them to table.

        row = 0
        for r in table_data:
            col = 0
            for item in r:
                cell = QTableWidgetItem(str(item))
                table.setItem(row, col, cell)
                col += 1
            row += 1

Code above will add data to table from (0,0) to (3, 4).

Then we can add a table to a layout to show.

The full code is here:

import sys
from PyQt5.QtWidgets import QApplication, QWidget,QPushButton, QHBoxLayout, QVBoxLayout
from PyQt5.QtWidgets import QLineEdit
from PyQt5.QtWidgets import QTableWidget,QTableWidgetItem
class YuTextFrame(QWidget):

    def __init__(self):
        super().__init__()
        self.initUI()

    def initUI(self):
        self.setWindowTitle('TutorialExample Add QTableWidget Example')  
        vbox = QVBoxLayout()
        
        # add table 
        table = QTableWidget(self)
        table.setColumnCount(5)
        table.setRowCount(10)
        #set table header
        table.setHorizontalHeaderLabels(['id','Name','Age','Sex','Address'])
        #add data
        table_data = []
        item_1 = ['001', 'John', 30, 'Male', 'Street No 2']
        item_2 = ['002', 'Lily', 32, 'Female', 'Street No 1']
        item_3 = ['003', 'Kate', 20, 'Male', 'Street No 3']
        item_4 = ['004', 'Tom', 22, 'Male', 'Street No 4']
        table_data.append(item_1)
        table_data.append(item_2)
        table_data.append(item_3)
        table_data.append(item_4)
        
        row = 0
        for r in table_data:
            col = 0
            for item in r:
                cell = QTableWidgetItem(str(item))
                table.setItem(row, col, cell)
                col += 1
            row += 1
        vbox.addWidget(table)

        self.setLayout(vbox)
        self.setGeometry(300,400,500,400)
        self.show()

if __name__ == '__main__':

    app = QApplication(sys.argv)
    frame = YuTextFrame()
    sys.exit(app.exec_())

Leave a Reply

Your email address will not be published. Required fields are marked *