- All (478)
- jom (0)
- Qt Linguist (7)
- Qt Eclipse Integration (9)
- Qt Designer (7)
- Qt Creator (4)
- Qt build system: qmake (31)
- Qt build system: configure (3)
- Qt Assistant (5)
- Printing (4)
- Porting from Qt 3 to Qt 4 (1)
- Plugins (7)
- Qt Visual Studio AddIn (2)
- Qt/MFC Migration (2)
- QtScript (3)
- MDI (2)
- XML (1)
- Widgets (22)
- WebKit (5)
- Tools and Containers (2)
- Threads (2)
- Text Handling (10)
- SQL (6)
- QtTest (1)
- QtService (1)
- Platform: Windows (49)
- Platform: Unix (16)
- Platform: Mac OS X (18)
- Image Formats (2)
- I/O (2)
- Graphicsview (8)
- Font handling (9)
- Event System (18)
- Drag and Drop (4)
- Dialogs (6)
- Desktop integration (3)
- ActiveQt (3)
- Itemviews (60)
- Layout (4)
- Qt Quick (10)
- Qt SDK (1)
- Licensing (2)
- Platform: Embedded Linux (38)
- Painting (32)
- OpenGL (4)
- Object Model (6)
- Network (5)
- Multimedia (3)
- Miscellanous (23)
- Main Window (19)
- Look and Feel (23)
- Development (0)
- Getting Involved (0)
- Routines (0)
How can I implement my own proxy model that is more advanced than just sorting/filtering
In cases such as being able to hide the top level items but yet still see the children in an itemview is where you would want to use a proxy model. However QSortFilterProxyModel by itself is not adequate enough for the functionality needed.
Therefore you need to subclass QAbstractProxyModel instead which enables you to have full control over the way that the proxy model represents the items from the source model.
Attached to this solution is an example which shows how to handle this for a case where you want to hide all items except for the children (and grandchildren) of a particular item. Since we want to hide a top level item here, we need to actually ensure that the proxy items end up with new parents since the hierarchy has changed.
To do this in a way that makes it easy to manipulate the hierarchy in any way you want we have created two maps, one for the mapping of a source index to its corresponding proxy index. The other map is for mapping a proxy index to its new parent as represented by the original source index.
The reason behind mapping a proxy index to a parent index in the source model is to facilitate placing proxy indexes whereever we want them to be without having to be concerned with not having mapped the corresponding source index yet. Therefore we can easily map a child of an index in the source model to be the parent of that index in the proxy model.
The example uses QStandardItemModel to simplify things on the source model side, but you can change the model in use on the source model and just tweak the fixModel() function to handle your own model instead.

4 comments
August 6, 2010
Ant Farmer
Attachment is missing so I’ve put it in the wiki here [developer.qt.nokia.com].
March 14, 2011
Lab Rat
If I remove
April 12, 2011
Lab Rat
People interested in achieving this task(hide all items except for the children) may also like to look at this blog entry http://lynxline.com/jongling-qt-models/
May 11, 2011
Lab Rat
The example has been updated now to show more of a reason behind why the
check is there because it is really to show for demonstration purposes.