9. Table Widgets
Table widgets in Qt provide a structured way do display and interact with tabular data.
QTableWidgetis a self-contained widget that includes its own data storage, allowing developers to easily manipulate items directly within the widget.QTableViewis part of the Qt model-view framework, requiring an external model to supply data, promoting separation of concerns and reusability in larger applications.
Both support sorting, resizing columns and rows, and selecting multiple cells. These widgets are commonly used in applications requiring grid-based input or visualization, such as data entry forms or result displays.
9.1 QTableWidget
[TODO: INHERITANCE TREE]
The QTableWidget class provides an item-based table view with a default model. Simply put, it’s a table. The items in a QTableWidget are provided by QTableWidgetItem objects.
![]() |
You need to enable your boss to create and edit a service comparison matrix for your company. |
To use a table widget in your application:
Create a
QTableWidgetinstance and set the table dimensions. The table in the example has five rows and four columns. We also add a label to the window and a push button for exporting the comparison matrix.Fill the table cells with data. Each table cell is represented with a
QTableWidgetIteminstance so we use a nested loop to create the items. We set each item’s data from a two-dimensional list usingQTableWidgetItem.setData(). After that, eachQTableWidgetItemis assigned to a table cell usingQTableWidget.setItem(row, column, item).Create two slot methods: one connected to
QTableWidget’sitemChanged()signal and the other connected to the push button’sclicked()signal. The first slot updates the label text when a table item is changed, and the second one ‘exports’ the matrix by printing it. The table is editable; if the user double-clicks a cell, changes its value, and presses Enter, the label’s text is updated accordingly.

A key distinction between QTableWidget and QTableView is in their handling of data. QTableWidget is well-suited for semi-structured or heterogeneous data, where individual cells can vary in type or format without column consistency - for instance, allowing a user to enter ‘Maybe’ in a boolean-like field instead of strictly True or False. In contrast, QTableView, paired with a model, is suitable for use with structured data, as model typically enforce uniform data types and validation rules per column. This flexibility makes QTableWidget ideal for quick prototypes and ad-hoc tables, while QTableView supports more scalable, data-driven applications.
9.2 QTableView
[TODO: INHERITANCE TREE]
QTableView is the default model-view class for displaying tabular data, and just as QListWidget is a subclass of QListView, QTableWidget is a subclass of QTableView.
![]() |
You users need to edit a table of aggregate economic indicators and select which ones to include in the report. |
To use QTableView in your application:
Create a
QTableViewinstance. We also add a label to the window to notify users of changes.Create a model instance. In the example, we use
QStandardItemModel, populating it with data from the two- dimensionaldatalist. Each item is represented with aQStandardItemobject. We set the data usingsetData()forDisplayRoleand adjust flags to make the first column read-only.Set the view’s model using
QTableView.setModel(). Connect the model’sitemChanged()signal to a slot to handle updates.

This separates the data model from the view, allowing the same model to be used with multiple views, and allowing a view’s model to be changed dynamically.
