PDA

View Full Version : Groups inside QTreeView (using QAbstractItemModel)



gabo
8th December 2020, 10:55
Hey all,

I have a tree model with several columns. It all works great but I now would like to group some rows into groups/categories. I can create a parent to act as the group and place all the rows as children, that's also fine. The problem is that I would like this "group" row to be independent of the columns so that it can span the whole row if needed. I would like something very similar to what is done in Qt Creator's property panel, like this:

13578

In the image, notice how "QAbstractScrollArea" and "QAbstractItemView" span the whole row, regardless of the size of the columns. That's exactly what I want to achieve.

I tried to look into Qt Creator's code but couldn't get to the right place where this is done. I also tried playing with the delegates but I am not sure if this will cause problems down the line.

Any suggestions on how to do this?

Thanks!

d_stranz
8th December 2020, 16:50
There is a QTreeView::setFirstColumnSpanned() method that will do what you want, I think.

gabo
11th December 2020, 08:52
That actually works. Thanks a lot!

A bit of a weird API forcing it to be only the first column, but I can get around it. Also seems to be reset when the model is reset which is also annoying.

d_stranz
11th December 2020, 16:38
Also seems to be reset when the model is reset which is also annoying.

Well, yeah. A model reset tells all views that everything they know is wrong, and they have to go back and ask for it all again. And the method lives in the view and not the model because you may not want all views of the model to have this behavior.

The best solution is to probably derive from QTreeView and override reset() and/or dataChanged(). Call the base class method first, then add your first row spanning.

gabo
14th December 2020, 11:33
I ended up connecting to the rowsInserted/dataReset signals from the model and fix-up the spanning for the first column accordingly. It is sufficient for my case now and it looks quite good.

Thanks a lot again