0

In Qt I have a sqlite database which I'm pulling in. One of the tables (configTable) has a QSqlTableModel attached.

The table has a simple 2-column key/value structure. The keys are strings with folder-like values such as "general/name", "general/version", "foo/bar/baz", etc. Values are just arbitrary variants.

I'd like to display this data in an easier-to-browse QTreeView instead of a QTableView, as my key structure lends itself very nicely to that.

Before I go reimplementing classes and all sorts of crazy things - is there an elegant solution to this? And if I reimplement or extend classes, which ones should I look at?

Thank you.

ymoreau
  • 3,402
  • 1
  • 22
  • 60
Kver
  • 767
  • 5
  • 19

2 Answers2

1

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.

ymoreau
  • 3,402
  • 1
  • 22
  • 60
-1

AFAIK you can make it only manually.

Basically, because how did you think Qt knows how to convert your data into tree model.

RazrFalcon
  • 787
  • 7
  • 17