Results 1 to 9 of 9

Thread: QTreeView: auto-resize

  1. #1
    Join Date
    Jan 2011
    Posts
    55
    Thanks
    12
    Qt products
    Qt4
    Platforms
    MacOS X Windows

    Default QTreeView: auto-resize

    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:

    qtrees.jpg

    Probably using a QGridLayout and putting each QTreeView into each grid cell? Thanks
    Last edited by papillon; 18th October 2011 at 11:20.

  2. #2
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,230
    Thanks
    302
    Thanked 864 Times in 851 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default 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.

  3. #3
    Join Date
    Jan 2011
    Posts
    55
    Thanks
    12
    Qt products
    Qt4
    Platforms
    MacOS X Windows

    Default 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.

  4. #4
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,230
    Thanks
    302
    Thanked 864 Times in 851 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default 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.

  5. #5
    Join Date
    Jan 2011
    Posts
    55
    Thanks
    12
    Qt products
    Qt4
    Platforms
    MacOS X Windows

    Default 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.

  6. #6
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,230
    Thanks
    302
    Thanked 864 Times in 851 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default 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.

  7. #7
    Join Date
    Jan 2011
    Posts
    55
    Thanks
    12
    Qt products
    Qt4
    Platforms
    MacOS X Windows

    Default Re: QTreeView: auto-resize

    Thanks, I'd really appreciate it.

  8. #8
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,230
    Thanks
    302
    Thanked 864 Times in 851 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default 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.

    MultiModel.zip

  9. The following 2 users say thank you to d_stranz for this useful post:

    papillon (23rd October 2011), Saxomophon (9th January 2013)

  10. #9
    Join Date
    Jan 2011
    Posts
    55
    Thanks
    12
    Qt products
    Qt4
    Platforms
    MacOS X Windows

    Default 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!

Similar Threads

  1. auto-resize trouble
    By zgulser in forum Qt Tools
    Replies: 9
    Last Post: 28th December 2008, 21:03
  2. Auto resize QLabel
    By munna in forum Newbie
    Replies: 12
    Last Post: 6th August 2008, 13:39
  3. QTextEdit auto resize
    By bunjee in forum Qt Programming
    Replies: 4
    Last Post: 26th October 2007, 19:59
  4. auto resize custom widget
    By CopyrightPhilly in forum Newbie
    Replies: 1
    Last Post: 5th October 2007, 09:40
  5. How to get a QDockWidget to auto resize?
    By bitChanger in forum Qt Programming
    Replies: 4
    Last Post: 6th January 2006, 16:10

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.