In this PyQt5 article we want to learn about Working with Tables and Models in PyQt5, PyQt5 is popular Python library for building GUI applications, it provides functionality for working with tables and models. in this article we want to talk about the power of tables and models in PyQt5.
Before talking about tables and models, it is good to understand the Model View Controller (MVC) pattern. In PyQt5, tables and models are typically implemented using the MVC architecture. we can say that model represents the data, the view displays the data, and the controller handles the communication between the model and the view.
So now let’s start our example, in this example we are going to create an empty table view with window.
1 2 3 4 5 6 7 8 9 10 |
from PyQt5.QtWidgets import QApplication, QMainWindow, QTableView app = QApplication([]) window = QMainWindow() window.setWindowTitle("Geekscoders.com") table_view = QTableView() window.setCentralWidget(table_view) window.show() app.exec_() |
If you run the code, this will be the result.

For populating the table view with data, you need a table model. PyQt5 offers the QAbstractTableModel class, you can sub class from this class for creating custom models, because using that class you can implement the necessary methods like rowCount(), columnCount(), data() and headerData().
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
from PyQt5.QtCore import QAbstractTableModel, QVariant class MyTableModel(QAbstractTableModel): def rowCount(self, parent): # Return number of rows in the table return 4 def columnCount(self, parent): # Return number of columns in the table return 3 def data(self, index, role): # Return data to be displayed at the specified index if role == Qt.DisplayRole: return f"Row {index.row()}, Col {index.column()}" return QVariant() def headerData(self, section, orientation, role): # Return headers for rows and columns if role == Qt.DisplayRole: if orientation == Qt.Horizontal: return f"Column {section}" else: return f"Row {section}" return QVariant() model = MyTableModel() table_view.setModel(model) |
In the above example, we have created a custom model subclassed from QAbstractTableModel. after that we implements the necessary methods to define the number of rows and columns, and we provides data for each cell, and set the headers for rows and columns.
This is the complete code for adding data, this example creates MyTableModel class that subclasses QAbstractTableModel. after that it implements the necessary methods (rowCount(), columnCount(), data() and headerData()) to define the structure and content of the table. QTableView widget is used to display the table, and the MyTableModel instance is set as the model for the table view.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 |
from PyQt5.QtWidgets import QApplication, QMainWindow, QTableView from PyQt5.QtCore import Qt, QAbstractTableModel, QVariant class MyTableModel(QAbstractTableModel): def rowCount(self, parent): return 4 def columnCount(self, parent): return 3 def data(self, index, role): if role == Qt.DisplayRole: return f"Row {index.row()}, Col {index.column()}" return QVariant() def headerData(self, section, orientation, role): if role == Qt.DisplayRole: if orientation == Qt.Horizontal: return f"Column {section}" else: return f"Row {section}" return QVariant() if __name__ == '__main__': app = QApplication([]) window = QMainWindow() window.setWindowTitle("Geekscoders.com") table_view = QTableView() model = MyTableModel() table_view.setModel(model) window.setCentralWidget(table_view) window.show() app.exec_() |
Run the complete code and this will be the result
