PDA

View Full Version : Looking for examples of subclassing a QTreeView



kgimpelson
23rd February 2011, 22:37
I'm looking for an example of subclassing, and extending a view widget. Bonus points if it is in pyqt4, but I can follow C examples.

I have a working data model (currently subclassed from qstandarditemmodel) that has both a tree like structure and a table like structure. IE, most items can be placed into column 1 with some hierarchy of parenting. These items all have vertical header information. However, some items should be grouped as multiple columns in a single row-- in this case the items have horizontal header information.

I've dug around the internet and bugged my workmates; while there are plenty of examples of subclassing data models, I have yet to find an example or tutorial for subclassing a view widget.

This is what I'd like to do:

My first choice for displaying all this would be to add a vertical header to qtreeview. I've reimplemented qstandarditemmodel.headerData() in my data model to return an appropriate vertical header information for an item of arbitrary depth in column 1, however when I apply this to a free-standing qheaderview, it seems to think it needs to display only the invisibleRoot.childCount() number of items (ie, it doesn't count the number of items available if fully expanded). I haven't a clue how to intercept that count so I could show/hide header information as the tree expands and collapse. Nor do I have a clue how to make actually add the vertical headerView into the treeview subclass, rather than tacking it on as an external part of the layout.

Okay, so that is the kind of stuff that I'd like to be able to do. Are there any examples of View subclassing out there that might give me some guidance?

Much appreciated.

tbscope
24th February 2011, 06:13
There's a very nice example of subclassing a view:
http://doc.qt.nokia.com/4.7/itemviews-chart.html

But I would first try if it is possible to use a delegate.
You also might want to find out if you can just put another treeview inside an index, that would make it a lot easier to implement.

nightghost
24th February 2011, 10:23
You've luck. The book Advanced Qt Programming has a chaper about this issue and that chaper is available as sample (http://www.informit.com/store/product.aspx?isbn=0321635906) (click "Sample Content")

kgimpelson
24th February 2011, 22:28
Thanks all-- I've already got a delegate managing the various appearances and editors for my different items. I think the example and the book excerpt will be very helpful; indeed it sounds like customizing a view is what I want for changing the "bigger picture."