Qt wiki will be updated on October 12th 2023 starting at 11:30 AM (EEST) and the maintenance will last around 2-3 hours. During the maintenance the site will be unavailable.
QML and QSqlTableModel
QML and QSqlTableModel
I wrote a simple class derived from QSqlRelationalTableModel to facilitate the use of such classes with QML, without manually defining the roles.
If you want to understand why roles are so important when working with SQL table models and QML, have a look at this explained code snippet [qt.io]
The data method returns the value of the role used. If there is a relationship in the table, i search the column with relation and return the correct value.. Note that empty string is returned when the item is not found and not QVariant value, in order to have a correct visualization in QML.
The generateRoleNames method creates the roles called as the table columns specified in the header.
Example:
Tables:
main.cpp
In qml file
——————————————————-
Note1:
Nice post, however the performance can be tweaked. data() will be called very often. Most of the processing and object instantiation should be done only once.
Note2:
I modified provided code according to this working example [stackoverflow.com] which was based on this wiki page