Vaska el gato
Vaska el gato

Reputation: 198

Display one data tree in two different views

enter image description here

Hello everyone.

  1. I have a tree of items as shown on image above (Items tree).
  2. I need to display this tree in a way that is shown on an image (Views). It has two views, Tree View and List view. Tree view should display the whole tree of items, and list view should display properties for a selected item in a tree view. (On an image it displays properties for a cat item)
  3. The way it is implemented right now is displayed at "Current implementation" part of an image. TreeModel contains item tree, and when you click on an item in a TreeView, current item is sent to a ListModel to display it's properties.

The issue thagt i encounter here is that in both views items are editable. In a tree view you can edit item's names, and this change should appear in a list view as well. The same goes for opposite direction. Changing name of a Cat item should reflect on a treeView cat item. Since it is two different models, the change is made only when you hover over another view. What i currently did is for a changed item in one of views, i search for a item in a another view via QModelIndex::match() and then just update that part of a view via emit dataChanged(). I am not sure this a good way. So if maybe you can give me some ideas on how this can be done better. Thank you.

Upvotes: 1

Views: 206

Answers (1)

You should have only one model. The QAbstractItemView::setRootIndex method is all you need: you can set the current item in the tree as the root for the table view. Alternatively, you could use a proxy viewmodel to adapt the data for display in a particular form.

Upvotes: 1

Related Questions