Results 1 to 5 of 5

Thread: Groups inside QTreeView (using QAbstractItemModel)

  1. #1
    Join Date
    Dec 2020
    Posts
    4
    Thanks
    1
    Qt products
    Qt5
    Platforms
    Windows

    Question Groups inside QTreeView (using QAbstractItemModel)

    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:

    groups.jpg

    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!

  2. #2
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,248
    Thanks
    304
    Thanked 867 Times in 854 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Groups inside QTreeView (using QAbstractItemModel)

    There is a QTreeView::setFirstColumnSpanned() method that will do what you want, I think.
    <=== The Great Pumpkin says ===>
    Please use CODE tags when posting source code so it is more readable. Click "Go Advanced" and then the "#" icon to insert the tags. Paste your code between them.

  3. The following user says thank you to d_stranz for this useful post:

    gabo (11th December 2020)

  4. #3
    Join Date
    Dec 2020
    Posts
    4
    Thanks
    1
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Groups inside QTreeView (using QAbstractItemModel)

    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.

  5. #4
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,248
    Thanks
    304
    Thanked 867 Times in 854 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Groups inside QTreeView (using QAbstractItemModel)

    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.
    <=== The Great Pumpkin says ===>
    Please use CODE tags when posting source code so it is more readable. Click "Go Advanced" and then the "#" icon to insert the tags. Paste your code between them.

  6. #5
    Join Date
    Dec 2020
    Posts
    4
    Thanks
    1
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Groups inside QTreeView (using QAbstractItemModel)

    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

Similar Threads

  1. QTreeView with QAbstractItemModel slow
    By Qiieha in forum Qt Programming
    Replies: 1
    Last Post: 7th December 2012, 10:06
  2. Performance with QTreeView and QAbstractItemModel
    By Goug in forum Qt Programming
    Replies: 2
    Last Post: 23rd November 2011, 01:30
  3. QTreeView with QAbstractItemModel
    By Fletcher in forum Qt Programming
    Replies: 4
    Last Post: 11th December 2009, 00:02
  4. Need help with QTreeView/QAbstractItemModel
    By Valheru in forum Qt Programming
    Replies: 5
    Last Post: 28th August 2006, 16:09
  5. [QT4] QTreeView, QAbstractItemModel and sorting
    By KShots in forum Qt Programming
    Replies: 3
    Last Post: 24th March 2006, 21:16

Tags for this Thread

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.