jetru
jetru

Reputation: 1993

Qt Table and Tree View with the same model

I have a neat Model based off QAbstractItemModel. This has a simple hierarchical tree structure which works perfectly for QTreeView. However, I want the QTableView/QListView to access and display only the leaf nodes( ALL leaf nodes ). What is the best way to do this? I don't want to rebuild the model( because it will be expensive ) and I do not want to have two models( same reason ).

Upvotes: 3

Views: 1760

Answers (2)

László Papp
László Papp

Reputation: 53173

I think you will need these steps to achieve this:

  1. Flatten a tree model into a list or table model.
  2. Remove the rows having children.
  3. Sort the list.

So, you could just wrap this flattening model inside a QSortFilterProxyModel.

QFlattenProxyModel is work in progress to be included in Qt:

https://bugreports.qt.io/browse/QTBUG-117716

Upvotes: 0

Gianni
Gianni

Reputation: 4390

You could create a proxy model. A class that sits between the View and the Model and filters out all of the non-leaf nodes and then just forwards the function calls to the original model for the leaves.

Upvotes: 1

Related Questions