Re: QTreeView: auto-resize
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.
Re: QTreeView: auto-resize
I would love to have a single QTreeView with multiple model sources (see my other post: http://www.qtcentre.org/threads/1310...ultiple-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.
Re: QTreeView: auto-resize
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.
Re: QTreeView: auto-resize
I really would like to try a proxy with multiple models. Problem is that I couldn't find a working example to study.
Re: QTreeView: auto-resize
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.
Re: QTreeView: auto-resize
Thanks, I'd really appreciate it.
1 Attachment(s)
Re: QTreeView: auto-resize
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.
Attachment 7025
Re: QTreeView: auto-resize
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: