
- PyQt - Home
- PyQt - Introduction
- PyQt - Environment
- PyQt - Hello World
- PyQt - Major Classes
- PyQt - Using Qt Designer
- PyQt - Meta Objects
- PyQt Signals & Slots
- PyQt - Signals and Slots
- PyQt - Support and Signals
- PyQt - Unbound and Bound Signals
- PyQt - New Signals with PyQtSignal
- PyQt - Connecting, Disconnecting, & Emitting Signals
- PyQt - Slot decorator
- PyQt - Slot Connection
- PyQt Layouts
- PyQt - Layout Management
- PyQt - QBoxLayout
- PyQt - QGridLayout
- PyQt - QFormLayout
- PyQt - QHBoxLayout
- PyQt - QVBoxLayout
- PyQt - QStackedLayout
- PyQt - QGraphicsGridLayout
- PyQt - QGraphicsAnchorLayout
- PyQt - QGraphicsLayout
- PyQt - QGraphicsLinearLayout
- PyQt Basic Widgets
- PyQt - Basic Widgets
- PyQt - Qlabel Widget
- PyQt - QLineEdit Widget
- PyQt - QPushButton Widget
- PyQt - QRadioButton Widget
- PyQt - QCheckBox Widget
- PyQt - QComboBox Widget
- PyQt - QSpinBox Widget
- PyQt - QMessageBox
- PyQt - QDialogButtonBox Widget
- PyQt - QFontComboBox Widget
- PyQt - QDoubleSpinBox Widget
- PyQt - QToolBox Widget
- PyQt - QMenuBar, QMenu & Qaction Widgets
- PyQt - QToolTip
- PyQt - QInputDialog Widget
- PyQt - QFontDialog Widget
- PyQt - QDialog Widget
- PyQt - QFileDialog Widget
- PyQt - QTab Widget
- PyQt - QSplitter Widget
- PyQt - QDock Widget
- PyQt - QStatusBar Widget
- PyQt - QTabBar
- PyQt - QList Widget
- PyQt - QScrollBar Widget
- PyQt - QProgressBar
- PyQt - QCalendar Widget
- PyQt - QMessageBox Widget
- PyQt - QPlainTextEdit
- PyQt - QDateEdit
- PyQt - QDateTimeEdit
- PyQt - QTimeEdit
- PyQt - QTextEdit
- PyQt - QTextBrowser
- PyQt - QScrollArea
- PyQt - Drag and Drop
- PyQt - Multiple Document Interface
- PyQt - QDialog Class
- PyQt Views
- PyQt - QColumnView
- PyQt - QTableView
- PyQt Drawing API
- PyQt - Drawing API
- PyQt - Drawing a Line
- PyQt - Drawing a Rectangle
- PyQt - Drawing a Triangle
- PyQt - Drawing a Circle
- PyQt - Drawing a Ellipse
- PyQt - Drawing a Polygon
- PyQt - Geometric Transformation
- PyQt - Drawing Effect
- PyQt Groups
- PyQt - QButtonGroup
- PyQt - QGroupBox
- PyQt Effects
- PyQt - Effects
- PyQt - Opacity Effect
- PyQt - QGraphicsBlur Effect
- PyQt - QGraphicsColorize Effect
- PyQt - QGraphicsDropShadow Effect
- PyQt Events
- PyQt - Event Handling
- PyQt - Drag & Drop Events
- PyQt - File Open Event
- PyQt - Action Event
- PyQt - Hide Event
- PyQt - Resize Event
- PyQt Database
- PyQt - Database Handling
- PyQt Essentials
- PyQt - BrushStyle Constants
- PyQt - QClipboard
- PyQt - QPixmap Class
- PyQt Useful Resources
- PyQt - Quick Guide
- PyQt - Useful Resources
- PyQt - Discussion
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 −

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 −
