PDA

View Full Version : Problem with QTreeWidget.setItemDelegateForRow



photo_tom
25th May 2010, 20:14
I have a QTreeWidget where I'm creating custom editing delegates for some rows in the tree that are based on field specific comboboxs. This part works fine.

My problem is when I go to assign the editor for the row. The code I'm using is -


_rowCount++;
_workingTreeWidget->setItemDelegateForRow(_rowCount, comboEdit);


Where _rowCount is the running row counter for the rows inserted into the tree structure from the start and comboEdit is the custom editor for this row only. My problem is that this is not working correctly. What I need to know is how to retrieve the correct row number that I've inserted the QTreeWidgetItem into, so that I can set the correct delegate for it.

jryannel
25th May 2010, 21:28
Not sure, but did you try QTreeWidgetItem::indexOfChild() on the parent item, or if it's a top-level item, you should be able to ask invisible root item.

photo_tom
25th May 2010, 21:57
A good idea, but doesn't work. After some experimenting, it didn't work and I see why.

I'm building a multi-level tree list with a varying number of indents depending on data being presented (which is why I'm using trees). What happeans is that the delegate is inserted into a map table by row and delegate for the QTreeWidget. It is not tied to level of indenent of position from start of tree. However, when the tree is rendered, it looks like a new row counter is used every time a new level is generated.

So if I insert a delegate somwhere in the middle of the tree at the first node after a new root node, that delegate gets applied to ALL nodes that are the first node in thier sub-trees. Thereby, duplicating my custom row editor in many, many places where I don't want it. Instead of being just the single instance that I want.

The way I see this, this acts like a bug in the design of this class. But more likely, a lack of understanding on my part.

----------------------------------------
EDIT - After I posted this, I found this in the reported bug list at http://bugreports.qt.nokia.com/browse/QTBUG-426 Dated 18 Jan 2007 and is not scheduled to be fixed. So it is time to look into another way to do this.

jryannel
26th May 2010, 08:37
Okay. Seems you have different data types on different hierarchies on your tree. One option I could see would be to have the item delegate be aware of the models item type. Here are the steps:

(1) Define a new ItemRole e.g enum { TypeRole = Qt::UserRole + 1 };
(2) Define an enums on the types you want to differentiate between
(3) For each tree item set your type with setData(TypeRole, YourType);
(4) Create a CustomItemDelegate and create your editor based on your type (you get an index as a parameter, just ask the index.data(TypeRole).toInt() to get your type back

Not sure how else to transport custom type information from model over to the delegate. Please let me know if this works. Thanks

photo_tom
2nd June 2010, 14:52
The custom user role was a good idea, but I could not make it work. Still not sure why.

However, while looking at the star delegate demo app, I saw that it used a custom variant data type. So I re-wrote things to that I had a custom data type that transfer both desired index for the combobox and the stringlist to generate the combobox from. Once I did this and then line for line adapted the demo app, I got things to work.

So for now, I think the only way to do this is
1 - Assign a editing delegate to the entire column
2 - In that delegate, when a custom data type is found, generate the correct editing control for your needs.