PyQt5 Tutorials
About Lesson

In this Python PyQt5 lesson, we want to learn about Python PyQt5 QTableWidget, so using pyqt5 qtablewidget you can create tables in rows and columns.

 

In this lesson we are going to create two examples on pyqt QTableWidget class, the first example is using Qt Designer and the second one is using coding.  you can just write pyqt5designer in your terminal, after opening the Qt Designer you need to create Widgets window. now we add widgets in Qt Designer.

  • Add a Vertical Layout in the Qt Designer 
  • Add a TableWidget item in the VBoxLayout 

 

 

So now you can add rows, columns and also items in your QTableWidget. right click on the tablewidget and click on the Edit Items, and from there you can add columns. so these are our columns.

QTableWidget Column Design
QTableWidget Column Design

 

 

These are rows for qtablewidget.

QTableWidget Rows
QTableWidget Rows

 

 

 

These are items for the qtablewidget.

QTableWidget Item
QTableWidget Item

 

 

 

Also you can add styles for the QTableWidget by right clicking on the tablewidget and choosing Change Stylesheet.

 

 

 

And this is our design.

PyQt QTableWidget Design
PyQt QTableWidget Design

 

 

 

After completing the design you need to save the .ui file, iam going to name it tablewidget.ui, now copy the file and paste it in the Scripts folder of your Python installation, because we want to convert our ui file in to python file and for converting you need to use pyuic5 module. pyuic5 module is located in the Scripts folder of your Python installation, run this command for converting  in your terminal.

 

 

 

And this is the converted code.

 

 

 

 

So now run the complete code and this is result.

PyQt5 TableWidget - Working with QTableWidget in PyQt
PyQt5 TableWidget – Working with QTableWidget in PyQt

 

 

Creating PyQt QTableWidget with coding

OK now let’s create our pyqt5 tablewidget using coding, this is the complete source code for creating QTableWidget.

 

 

These are our window requirements like window title, window icon, width and height of the window, x and y position of the window.

 

 

In here we have created our QTableWidget object also we need to set the row and column count for the tablewidget.

 

 

And using QTableWidgeItem class you can add items to the pyqt tablewidget. 

 

 

Run the complete code and this is the result.

Working with QTableWidget in PyQt
Working with QTableWidget in PyQt