PyQt5 QTableWidget Tutorial: Create a Dynamic Table

In this PyQt5 article we are going to learn about PyQt5 QTableWidget Tutorial: Create a Dynamic Table, so PyQt5 is powerful Python module for creating graphical user interfaces (GUIs) for desktop applications. one of the most commonly used widgets in PyQt5 is QTableWidget. QTableWidget is class in PyQt5 that provides table view with sortable and editable cells. In this tutorial we are going to learn how to create dynamic table using PyQt5’s QTableWidget.

 

Before we start, make sure that you have installed PyQt5 on your machine. you can install it using pip by running the following command in your terminal:

Now that we have PyQt5 installed, let’s create dynamic table using QTableWidget.

 

 

Step 1: Create basic PyQt5 application

The first step is to create a basic PyQt5 application. this can be done using following code:

This code creates basic PyQt5 application with blank window. we will add QTableWidget to this window in the next step.

 

 

Run the complete code and this will be the result 

PyQt5 QTableWidget Tutorial: Create a Dynamic Table
PyQt5 QTableWidget Tutorial: Create a Dynamic Table

 

 

Step 2: Add a QTableWidget to the application

Now that we have basic PyQt5 application, we can add QTableWidget to it. following code creates QTableWidget and adds it to the window:

This code creates QTableWidget with 4 rows and 2 columns and adds it to the application’s layout. we also set the window’s title, size and position.

 

 

Run the complete code and this will be the result 

PyQt5 QTableWidget Tutorial: Create a Dynamic Table
PyQt5 QTableWidget Tutorial: Create a Dynamic Table

 

 

Step 3: Populate the QTableWidget with data

Now that we have QTableWidget in our application, we can populate it with data. this code adds data to QTableWidget:

This code creates QTableWidget with 4 rows and 2 columns and populates it with data. the data is stored in list of tuples and we iterate through the list to add each tuple’s values to cell in the table. The QTableWidgetItem class is used to create the table cells, and the setItem() method is used to add the cells to the table.

 

 

Run the complete code and this will be the result.

PyQt5 QTableWidget Tutorial
PyQt5 QTableWidget Tutorial

 

 

Step 4: Make table dynamic

Now that we have a QTableWidget with data, we can make it dynamic. We can add rows and columns to the table as needed. The following code adds a button to the application that adds a row to the table when clicked:

The code adds button to application that adds row to the table when clicked. addBtn() method creates QPushButton and connects its clicked signal to addRow() method. addRow() method gets current number of rows in the table and increases it by one using the setRowCount() method. this makes the table one row taller, and the new row is added at bottom of the table.

You can now run the code and see that the table is dynamic. Clicking “Add Row” button will add new row to the table.

 

 

Learn More on Python

Leave a Comment