9. Table Widgets

Table widgets in Qt provide a structured way do display and interact with tabular data.

  • QTableWidget is a self-contained widget that includes its own data storage, allowing developers to easily manipulate items directly within the widget.

  • QTableView is 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.

An icon of a clipboard-list1

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:

  1. Create a QTableWidget instance 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.

  2. Fill the table cells with data. Each table cell is represented with a QTableWidgetItem instance so we use a nested loop to create the items. We set each item’s data from a two-dimensional list using QTableWidgetItem.setData(). After that, each QTableWidgetItem is assigned to a table cell using QTableWidget.setItem(row, column, item).

  3. Create two slot methods: one connected to QTableWidget’s itemChanged() signal and the other connected to the push button’s clicked() 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.

An icon of a clipboard-list1

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:

  1. Create a QTableView instance. We also add a label to the window to notify users of changes.

  2. Create a model instance. In the example, we use QStandardItemModel, populating it with data from the two- dimensional data list. Each item is represented with a QStandardItem object. We set the data using setData() for DisplayRole and adjust flags to make the first column read-only.

  3. Set the view’s model using QTableView.setModel(). Connect the model’s itemChanged() 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.