PDA

View Full Version : QTreeView different item delegate for child items possible?



Royceybaby
7th January 2010, 16:46
Hi,

How can I use a different item delegate for child items in my QTreeView?

ParentItemA
-ChildItemA
-ChildItemB
ParentItemB
-ChildItemC

I have found the function setItemDelegateForRow, but I don't know how to apply that just for the child items in my QTreeView. Can anyone give me a clue?

I have been building upon the Simple Tree Model example from the Qt sources.

Thanks,
Royce

LynneV
7th January 2010, 18:55
I'm kind of new to Qt programming... but I have been able to put different delegates on Tree item children. I made a single ItemDelegate subclassed from QStyledItemDelegate. I had to put a switch statement for each node/child type for each of the functions: createEditor, setEditorData, setModelData, Painter and maybe setEditorGeometry to indicate which editor/editor function/painting belongs with each node type. If you can use a default display/editor for a particular node's data (like plain text), you can just have a case for calling the functions from the parent (QStyledItemEditor::Paint for example). I am storing a user class in my model, so I have to paint everything (because the defaults don't have any idea what to do with the class data!)... if you have 'normal' data, you probably don't have to define the paint function. Actually, the star rating delegate example is a very good guide. You just have to look at the itemdelegate as a single class that contains every editor/drawing function you need. The painting takes place first, and is completely separate from editing, with the editor activating only when the user asks to change data. <<that's what I think so far, in any case>>. Hope it helps!

Royceybaby
7th January 2010, 19:31
I have a subclassed QItemDelegate that does some formatting for my columns already. So adding a switch in to draw the child and parent rows differently is not a problem.

The only thing I can't get my head round is how will the QItemDelegate::paint function know whether the item it is drawing is a parent item or child item and enter the correct switch path. I am sure it is simple but I just can't think how.

Royce

faldzip
7th January 2010, 20:58
I have a subclassed QItemDelegate that does some formatting for my columns already. So adding a switch in to draw the child and parent rows differently is not a problem.

The only thing I can't get my head round is how will the QItemDelegate::paint function know whether the item it is drawing is a parent item or child item and enter the correct switch path. I am sure it is simple but I just can't think how.

Royce

if you have only two levels then parent items have invalid parent (index.parent().isValid() gives false for top level items) and thier children have valid parents.

If your structure is more complicated then you can use Qt::UserRole and above to store your data:
With QStandardItemModel it would look like this:


// when creating an item you add:
item->setData(1, Qt::UserRole); // parent item
// or
item->setData(2, Qt::UserRole); // child

// then in delegate paint()
if (index.data(Qt::UserRole).toInt() == 1) {
// parent item
}
if (index.data(Qt::UserRole).toInt() == 2) {
// child item
}

And if you have you own model class then return proper value (in my example it is 1 or 2) for Qt::UserRole in your model data() method.

Royceybaby
7th January 2010, 21:14
Thank you both for your help. Been banging my head on my desk trying to work that out and now I understand a little bit more and have got it working!