PyQt - Table View



The QTableView class provides a model or view implementation that is used for displaying tabular data. It presents information in a grid format with rows and columns. It allows users to navigate and interact with data efficiently. This grid feature make QTableView useful for applications such as spreadsheets, data browsers, and database viewers.

Model/View Framework

The QTableView class is a part of Qt's model/view framework. This framework separates the representation of data (the model) from its visual presentation (the view) that allows for greater flexibility and reusability in GUI design. By connecting a data model to a table view we can populate the view with data from various sources and update it dynamically as needed.

Model Integration

Easy integration with models derived from the QAbstractTableModel class is one of the key feature of QTableView. This helps us to populate the table view with data from various sources that range from simple lists to complex database queries. By subclassing QAbstractTableModel we can create custom models that meet the requirement of the Model.

Methods of QTableView

Method Description
clearSpans() Clears the span information for cells in the table.
columnAt(int) Returns the column index at the given x position.
columnSpan(int, int) Returns the number of columns spanned by the cell at the given row and column.
columnViewportPosition(int) Returns the viewport position of the specified column.
columnWidth(int) Returns the width of the specified column.
currentIndex() Returns the model index of the current item in the view.
horizontalHeader() Returns the horizontal header for the table.
rowAt(int) Returns the row index at the given y position.
rowHeight(int) Returns the height of the specified row.
rowSpan(int, int) Returns the number of rows spanned by the cell at the given row and column.
rowViewportPosition(int) Returns the viewport position of the specified row.
setColumnWidth(int, int) Sets the width of the specified column.
setRowHeight(int, int) Sets the height of the specified row.
setSpan(int, int, int, int) Sets the span of the table.
setWordWrap(bool) Sets whether word wrapping is enabled for the table.
showGrid(bool) Sets whether to display grid lines in the table.
verticalHeader() Returns the vertical header for the table.
visualRect(QModelIndex) Returns the rectangle on the viewport occupied by the item at the given model index.

Example 1: Basic Usage

In the below example, we create a basic QTableView with 4 rows and 3 columns and populates it with dummy data using a QStandardItemModel.

import sys
from PyQt6.QtGui import QStandardItemModel, QStandardItem
from PyQt6.QtWidgets import QApplication, QTableView

app = QApplication(sys.argv)

# Create a standard item model
model = QStandardItemModel()

# Set row and column count
model.setRowCount(4)
model.setColumnCount(3)

# Populate the model with data
for row in range(4):
   for column in range(3):
      item = QStandardItem(f'Row {row}, Column {column}')
      model.setItem(row, column, item)

# Create and configure the table view
table_view = QTableView()
table_view.setModel(model)
table_view.setWindowTitle('Basic QTableView Example')
table_view.show()

sys.exit(app.exec())

Output

Aftet execution of above code, we get the following result −

pyqt qtableview example 1

Example 2: Customizing Appearance

In the below example, we customize the appearance of the QTableView by enabling word wrapping and hiding grid lines, improving readability and aesthetics.

import sys
from PyQt6.QtWidgets import QApplication, QTableView, QStandardItemModel, QStandardItem

app = QApplication(sys.argv)

# Create a standard item model
model = QStandardItemModel()

# Set row and column count
model.setRowCount(4)
model.setColumnCount(3)

# Populate the model with data
for row in range(4):
   for column in range(3):
      item = QStandardItem(f'Row {row}, Column {column}')
      model.setItem(row, column, item)

# Create and configure the table view
table_view = QTableView()
table_view.setModel(model)

# Customize appearance
table_view.setWordWrap(True)
table_view.setShowGrid(False)

table_view.setWindowTitle('Customized QTableView Example')
table_view.show()

sys.exit(app.exec())

Output

The above code produces the following result −

pyqt qtableview example 2
Advertisements