parkovski
parkovski

Reputation: 1523

QTreeView/QAbstractItemModel subtrees in multiple columns

I'm working on a subclass of QAbstractItemModel that plugs into a QTreeView. It has a recursive Name = Value type structure - any index could have its own subtree. This is fine on the left side, because almost every tree view out there works that way. The problem is that sometimes I want a subtree only on the right side - a list of values. As I have it right now, it seems like it should work, but Qt is never calling rowCount() for the right side, and never realizing that there should be a subtree there.

The solution that I have right now is basically to create a separate model for that, and use setIndexWidget to give it a separate tree view every time this happens. That's fine, but I'd really like to get the subtrees showing up on the right without having to throw tree views all over the place. My model responds that there are subtrees over there, but Qt just never asks for them.

If this is a little unclear, this is the basic idea of what I want to accomplish:

- Root        |
  - Name 1    |    Value
      Name 2  |  - Compound Value
              |      Sub-value 1
              |      Sub-value 2
      Name 3  |  + Compound Value (collapsed)
  + Name 4    |    Value

As it is, the compound values will not get the +'s and -'s next to them because Qt never calls hasChildren() or rowCount() in that column, even though my model would return that yes, there are children, if it was asked.

If I end up having to give it a sub-tree view, that's fine. I'd just like to be sure that there's not a better way to do it first.

Upvotes: 4

Views: 1257

Answers (1)

Carel
Carel

Reputation: 3397

I'm trying to implement a dual Tree View my self, some thing like

+ a        |    A
  + b      |  + B
    c      |      C

and from What I've seen you can make the space between Name 2 and Name 3 by returning empty data e.g. an empty string under the Name 2 which would enable you to have a + infront of it.

So something like this may help

def data(self, index, role): 
    ...
    if item.pathdepth() > 3 : 
        return " "
    ...

def flags(self, index)
    ...
    if item.pathdepth() > 3 : 
        return Qt.Some_Role but not others
    ...

I don't know enough about the roles yet, but you could disable selection and editing so that the 'blanks' are not selectabe by the user.

But I haven't worked out how to get a tree in the second column.

Upvotes: 0

Related Questions