QML and QSqlTableModel

From Qt Wiki
Revision as of 16:33, 14 January 2015 by Maintenance script (talk | contribs)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigation Jump to search

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

Categories: