Results 1 to 6 of 6

Thread: Dynamic Grouping of QTableView Rows

  1. #1
    Join Date
    Feb 2010
    Location
    Sydney, Australia
    Posts
    111
    Thanks
    18
    Thanked 5 Times in 5 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Dynamic Grouping of QTableView Rows

    I would like to be able to group the rows in a QTableView by a certain colum. For example, if I had a table showing:

    Item Price
    Hat 10
    Belt 5
    Suitcase 10
    Umbrella 5

    I would like to be able to group it by price, and the result would be something like

    Item
    + Price 10 (2)
    + Price 5 (2)

    And with the nodes expanded

    Item
    - Price 10 (2)
    Hat
    Suitcase
    - Price 5 (2)
    Umbrella
    Belt

    This would need to be implemented with a QTreeView, of course. Has anyone done this before? Are there any open source projects that does this that I could have a look at?

    Any help welcome.

    Thanks

  2. #2
    Join Date
    Jul 2009
    Location
    Enschede, Netherlands
    Posts
    462
    Thanked 69 Times in 67 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Dynamic Grouping of QTableView Rows

    There's several possibilities. You can work with the table model and implement a sort model that has the behavior you want (keeping the original table model the way it is), or you reimplement the source model. In both cases, have a look at the Editable Tree Model example. If you want to use the sort/proxy model approach, have a look at the KDE libraries. KDE is Qt based and they have some developers that are quite good at creating sort/filter proxy models.
    Horse sense is the thing that keeps horses from betting on people. --W.C. Fields

    Ask Smart Questions

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

    stefanadelbert (6th December 2010)

  4. #3
    Join Date
    Feb 2010
    Location
    Sydney, Australia
    Posts
    111
    Thanks
    18
    Thanked 5 Times in 5 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Dynamic Grouping of QTableView Rows

    Thanks for the reply, franz. I like the idea of implementing a sort proxy so that the underlying table model remains intact.

    I'm happy to take a look at the KDE libraries. Any chance you could point me at some code in particular that would be relevant? I use Gnome, so I'm unfamiliar with KDE and KDE apps that might have an implementation similar to what I'm after.

    Many thanks
    Stefan

  5. #4
    Join Date
    Jul 2009
    Location
    Enschede, Netherlands
    Posts
    462
    Thanked 69 Times in 67 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Dynamic Grouping of QTableView Rows

    Maybe check this article about an identity proxy model by one of the kde guys. You might want to check that out and see if it gives you a nudge in the right direction.
    Horse sense is the thing that keeps horses from betting on people. --W.C. Fields

    Ask Smart Questions

  6. #5
    Join Date
    Feb 2010
    Location
    Sydney, Australia
    Posts
    111
    Thanks
    18
    Thanked 5 Times in 5 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Dynamic Grouping of QTableView Rows

    Thanks, Franz.

    I spent some time last night going through the KDEUI lib and I found the KIdentityProxyModel and the KCategorizedSortFilterProxyModel, but unfortunately I don't see how they are going to help me yet. But they have given me some ideas.

    I have an underlying CAbstractListModel with an attached QTableView. What I'd like to do is view the data from a list model in a QTreeView with arbitrary grouping defined by the user. The user should be able to specify a column (only one at a time, i.e. no nested grouping) to group by and then each unique value in that column would become a new parent node with the relevant original rows are children. Also the "group by" column would be hidden. I found an working online ASP.NET example of the kind of functionality I'm talking about here.

    Maybe what I need to do is have a proxy model (inheriting from QSortFilterProxyModel perhaps) on top of the list model which provides extra functionality that is required by the QTreeView, e.g.

    QModelIndex parent(const QModelIndex &index) const;

    I want to create a generic solution because I have a few tables in my application that would benefit from this functionality. Unfortunately the priority of this job is not high at all so I'm not able to give it too much time right now. I'll post back with further ideas, progress, etc.

  7. #6
    Join Date
    Feb 2010
    Location
    Sydney, Australia
    Posts
    111
    Thanks
    18
    Thanked 5 Times in 5 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Dynamic Grouping of QTableView Rows

    I did some more searching and found this wiki entry, [WIKI]Transpose_proxy_model[/WIKI].

    I might just be getting closer to a possible answer. Having a specialised QAbstractProxyModel that is able to provide a QTreeView with the relevant hierarchical information could work. The trick is going to be working out the hierarchical details from a non-hierarchical model.

    I'm going to have to just try it out and see what happens.

Similar Threads

  1. Replies: 2
    Last Post: 23rd November 2010, 05:43
  2. Dynamic form layout - Ability to add new rows
    By FaradayCage in forum Newbie
    Replies: 1
    Last Post: 22nd July 2010, 16:23
  3. Replies: 0
    Last Post: 22nd April 2010, 13:18
  4. QTableView 2 rows inside every row
    By skuda in forum Qt Programming
    Replies: 3
    Last Post: 22nd April 2009, 09:23
  5. Replies: 6
    Last Post: 5th March 2006, 21:05

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.