Python PySide6
About Lesson

In this lesson we want to learn about Python PySide6 QTableWidget, QTableWidget is  subclass of the QAbstractItemView class and is used to display and edit tabular data. It is particularly useful when you want to display data in a table format, where each row represents an item or record, and each column represents a data field or attribute.

 

Creating a QTableWidget

For creating QTableWidget in PySide6, you can use the following code:

In this code we have created QTableWidget instance, set the number of rows and columns in the table, set the headers for the table, and populate the table with data using the setItem method. finally we have showed the table and run the application using the show and exec methods.

 

 

Run the code and this will be the result.

Python PySide6 QTableWidget
Python PySide6 QTableWidget

 

 

 

Styling a QTableWidget

By default QTableWidget has plain and unstyled appearance. however you can customize its appearance using style sheets. for example to change the background color of  QTableWidget, you can use the following code:

In this code, we set the background color of the QTableWidget to a light gray color using a style sheet.

 

 

You can also set font and font size of the headers and cells using style sheets. for example to set the font of the headers to bold and increase the font size of the cells you can use the following code:

In this code we set the font of the headers to bold using the QHeaderView::section selector and increase the font size of the cells using the QTableWidget selector.

 

 

This is OOP code for QTableWidget

In this code we have created TableWidget class that inherits from QWidget. TableWidget class takes the table data as an argument in its constructor and initializes the UI in the initUI method. initUI method creates QTableWidget instance, sets the number of rows and columns, sets the headers, populates the table with data and sets the layout.

For using TableWidget class we have created an instance of it with the table data and show it. The app.exec() method starts the event loop which allows application to run.