You have to do the parsing+mapping between the list of value/value/value
and a tree model yourself. But there is a (tricky) Qt way to do this yes.
The Qt Model-View architecture can represent many different structures of data, based on the QAbstractItemModel
class. A Qt model must implement some functions to tell the view : how many columns, row, children etc.
A list model (Qt provides QAbstractListModel
), is basically a model that says to the view :
- I have one root item (all data items are represented by a
QModelIndex
, root has an invalid parent)
- This root item has only one column
- This root item has as many rows as your list has elements
A tree model will return the appropriate children for each QModelIndex
. The abstract model of Qt actually allows each child item to be a table (QModelIndex
always has a parent and a row-column index).
Long story short, you have to create a proxy model (QAbstractProxyModel
or a suitable subclass, but for your need I don't think there is one). This proxy will transform the data your QSqlTableModel
is sending, and this is where you can tell the view that you actually have a tree and not a list.
Your root items are the items from your database list of keys (first element of the foo/bar/whatever
), but you need to regroup all the root items that has the same key.