basysKom AnwendungsEntwicklung

Modern TableView in QML: What’s New in Qt 6.8 and Beyond
Essential Summary
Over the years, the capabilities of QtQuick’s TableView have evolved dramatically-from early custom implementations to well supported feature in Qt 6.8 and newer. In this article we explore the progression of QtQuick/QML’s TableView, outline the limitations of early versions, and highlight newer features such as custom selection modes, header synchronization, and lightweight editing delegates. Check it out.
Professionelle Entwicklung Ihrer HMI

Sie benötigen ein Geräte-HMI, Ihnen fehlt es aber an Zeit oder speziellem HMI-Know-How?​

Warum Dienstleistungen von basysKom?

Von der Konzeption über die Implementierung bis zum Testen unterstützen wir Sie in der Entwicklung Ihrer individuellen HMI. Unsere Services umfassen zudem die Technische Beratung, individuelle Trainings, Coaching, Verstärkung Ihrer Entwicklungsteams bis hin zur vollständigen Auftragsabwicklung im gesamten Lebenszyklus Ihres Produktes.

Some history on TableView in QML

When implementing applications for our clients, one question that comes up consistently is whether some data could be displayed in a table-like format. Since the majority of our Qt-based projects make use of QtQuick for the UI, the answer often has been “yes, but…”.

The complexity of such a task has changed quite a lot over the years. With the flexibility of QML creating something like a table has been possible from the very start, it has mostly been a question of how much time one was willing to spend on implementing it. It made sense that at some point something like a TableView, similar to the existing view items, would be made available as part of the framework itself. So with Qt 5.1 (released in 2013) a first implementation of TableView was included in the QtQuickControls1 module (QtQuickControls2 didn’t exist until Qt 5.7). Back then the focus was more on providing appropriate alternatives to the objects that QtWidgets provided. Performance issues prevented or at least limited its usage for embedded or mobile uses cases, requiring potentially time consuming custom implementations.

When Qt 5.12 was released five years later, TableView finally became part of the QtQuick module and an addition to existing views like ListView and GridView. Although limited in its capabilities, it was a reasonable starting point for implementing common use cases like selecting cells, highlighting, resizing or moving rows or columns. With some additional effort one could also implement cell editing, since naively using TextInput elements for each cell slowed things down quite noticeably, especially for larger models. The overall performance also increased significantly, due to the TableView only creating the delegates that were currently visible.

Two years later Qt 5.15 added two essential view items to the QtQuickControls2 module: HorizontalHeaderView and VerticalHeaderView. They allowed adding appropriate horizontal and vertical headers to an existing TableView, without the need for custom implementations. Both of these views are TableViews themselves and can be easily synched with the actual table, resulting in the expected behavior when scrolling larger tables.

The current state

With Qt 6 several years later, further enhancements have found their way into the TableView. Selection is now customizable in several ways, allowing selecting by row, column or by individual cells by setting a selectionBehavior (Qt 6.4). This behavior can be further adapted by setting selectionMode to single, contiguous or extended mode (Qt 6.6). Rows and columns can also be resized (Qt 6.5), and, when used in combination with HorizontalHeaderView or VerticalHeaderView, even moved (Qt 6.8). One very significant enhancement was made regarding editing (Qt 6.5). A common editDelegate can now be specified, which will be instantiated when needed, according to the configured editTriggers. The purpose of the default delegate of the TableView is to only display the current cell value, which can now be done by a lightweight component. No need to use a TextInput for each cell just to make it editable or come up with a mechanism to track the cell that a user currently wants to edit. And if you’re going to use the recently released Qt 6.9, it becomes even simpler: the new TableViewDelegate component does most of the work for you and provides suitable editing functionality and visualization.

With these relatively new capabilities it has become much easier to implement a full-fledged table with almost all of the expected functionality. Very few things remain that require a custom implementation, sorting and filtering for example. Although the Qt.labs.qmlmodels module provides a TableModel type that serves its purpose for some very simple use cases, you’re most probably going to write your own model based on QAbstractTableModel, which you can then combine with a QSortFilterProxyModel, that can take care of sorting and filtering the data accordingly.

Conclusion

As we can see a lot has happened, especially with Qt 6, to allow implementing useful table representations with QML. If you want to try it out for yourself you can check out a small example project.

Picture of Martin Leutelt

Martin Leutelt

Martin Leutelt is a software engineer at basysKom with 15 years of experience in creating HMI applications. He has consulted customers from various industries regarding application development for desktop, embedded and mobile platforms. Although mainly using C++ and Qt/QML, he has employed web technologies as well. He holds a computer science diploma from the University of Koblenz.

Schreibe einen Kommentar

Deine E-Mail-Adresse wird nicht veröffentlicht. Erforderliche Felder sind mit * markiert.

Weitere Blogartikel

basysKom Newsletter

We collect only the data you enter in this form (no IP address or information that can be derived from it). The collected data is only used in order to send you our regular newsletters, from which you can unsubscribe at any point using the link at the bottom of each newsletter. We will retain this information until you ask us to delete it permanently. For more information about our privacy policy, read Privacy Policy