PDA

View Full Version : QTreeView: auto-resize



papillon
18th October 2011, 12:15
I have a QFrame containing multiple QTreeViews, layed in vertical order. I'd like to set up things so that when, for example, QTreeView2 is expanded, the QTreeViews below it would move further away so that they do not overlap.
Maybe this image will explain better what I need to do:

7005

Probably using a QGridLayout and putting each QTreeView into each grid cell? Thanks

d_stranz
19th October 2011, 01:56
Is there a reason why you need multiple tree views? If you had one tree, you would get the behavior you want automatically. I am not sure that a grid layout will enlarge and shrink like your picture when you expand or collapse one of the trees.

papillon
19th October 2011, 09:55
I would love to have a single QTreeView with multiple model sources (see my other post: http://www.qtcentre.org/threads/13105-QTreeView-with-multiple-models).
I've read that to achieve that I need to use a proxy model, adding all the sources I need to it.
But I couldn't find a working example to learn how to do it.

d_stranz
20th October 2011, 04:50
I think it will be some ugly coding, but you may be able to achieve what you want by putting your tree views into a customized list widget and manually setting list widget row heights (sizeHintForRow()) in response to expanded and collapsed signals from the trees.

Making a proxy that uses multiple models will probably be ugly too, but if each model corresponds to a single top-level tree item (row) then it could be doable. I think you'd basically have to implement the data() method in such a way that when a model index is passed with column 0, you access the model that corresponds to the row.

papillon
20th October 2011, 09:25
I really would like to try a proxy with multiple models. Problem is that I couldn't find a working example to study.

d_stranz
20th October 2011, 15:47
I started playing with this last night. Didn't use a proxy, just derived directly from QAbstractItemModel. Added a QVector of QAbstractItemModel pointers as a member. This vector holds the multiple models managed by the wrapper model. Got it working to the point where I could display the top-level tree (of wrapped model "names") and then expand to one level below that (the actual top-level content of each wrapped model). But I've done something wrong in the index(), parent(), or rowCount() methods because it wants to expand beyond that, and there is nothing in the wrapped models beyond the top level.

I'll play with it some more later, and post code if I get it working.

papillon
20th October 2011, 15:49
Thanks, I'd really appreciate it.

d_stranz
23rd October 2011, 22:19
Ok, I have something working. Code is in the attached zip file. The .pro file was generated from the Qt Visual Studio add-in, so I don't know if it is valid. I use Visual Studio projects with Qt. My VS project file is also in the zip.

The MultiModel class is derived from QAbstractItemModel, and is essentially a tree of tree models. When you add a tree model to it, it wraps the model, and then internally maps model indexes from the MultiModel to model indexes of the wrapped models.

There is a test program which creates multi-level QStandardItemModel trees.

Note that I have not been able to get this working with a QFileSystemModel as one of the wrapped models. If you create a QFileSystemModel and insert it directly into the QTreeView in the demo, everything works fine. If you add the same thing to the MultiModel as a wrapped model, it does not populate the file system tree as expected. There seems to be some differences between the way a QTreeView populates itself vs. the way I am doing it. I haven't dug into the QTreeView code enough to know what's wrong.

Anyway, hope this is enough to get you started. There will probably be methods you will want to add - for example, if one of the wrapped trees changes after it is wrapped, the MultiModel does not update to reflect the differences. To do that, it will need to slots to handle the QAbstractItemModel signals.

7025

papillon
23rd October 2011, 23:18
d_stranz, thanks so much for kindly providing this source code, much appreciated! I was really stuck...

Tomorrow I will study it and finally be able to use it in my plugin project! :rolleyes: