PDA

View Full Version : Access QTreeWidgetItem from a QTreeWidget's item delegate



redneon
23rd May 2009, 23:52
I have a QTreeWidget object on a form and I'm using an item delegate to allow me to override how the columns are displayed.

I want my second column to be a progress bar so in the item delegate's paint() method I override the option passed in with a new QStyleOptionProgressBar (as is the method used in the torrent example).

This all works well but from inside that paint() method I want to be able to access the relevant QTreeWidgetItem that it is painting and I can't figure out how to do it.

There are three objects passed in: a QPainter, a QStyleOptionViewItem and a QModelIndex.

In the torrent example it does something similar by using the row from the QModelIndex but I can't do that as I have rows within rows so there can be many instances of row 1, for example. Plus, it seems like a bit of a hack anyway.

There must be a way to do this but I've been reading the docs and looking at examples for a couple of hours and I can't figure it out.

Please help.

faldzip
24th May 2009, 00:05
row is relative to parent. And QTreeWidgetItem is provided for use instead of QModelIndex (QTreeWidget and Items are based on Model/View so they use QTreeView and QModelIndexes as well but you don't see it). But every QTreeWidgetItem has it corresponding QModelIndex containing the same data, and having the same row, column and parent (but QModelIndex parent). I don't really know what exactly you want to get from the item but i think that for example current progress shuold be store in the item and if you want to paint it just use index.data(YourRoleStoringProgress) and thats it.

redneon
24th May 2009, 10:27
That's exactly what I wanted, cheers. I'm now storing the progress in role 32 and it's working a treat.