-->

QTableView to display only leaves of a tree model

2019-07-05 19:34发布

问题:

Assume I have a tree structure (tree leaves in bold, sorry for the dots):


  • A
    • A1
    • A2
  • B
    • B1
      • B11
    • B2
  • C

stored in a QAbstractItemModel (with set parent/child relations).

How to display only the tree leaves in a QTableView?

Basic idea was to implement a QSortFilterProxyModel. Intuition suggested there would be a way to iterate through the tree and return a valid index if the item is a leaf, or QModelIndex() if not.

// QAbstractItemModel *model;
m_leavesModel.setSourceModel(model); // QSortFilterProxyModel m_leavesModel;
m_leavesTableView->setModel(&m_leavesModel);

At the moment I can only display "first level" items - A, B and C, just below the root.