PDA

View Full Version : How to drop as QTreeWidgetItem subclass



Olivier Berten
6th June 2010, 14:24
I'm implementing a drag-n-drop between a list and a tree where I have different sorts of QTreeWidgetItem subclasses. I want the dropped list item to be inserted as QTreeWidgetCustomItem instead of QTreeWidgetItem. How could I achieve that?

SixDegrees
6th June 2010, 15:39
You could derive your QTreeWidgetCustomItem from QTreeWidgetItem.

Olivier Berten
6th June 2010, 15:54
QTreeWidgetCustomItem is a subclass of QTreeWidgetItem with additional features. Now when I drop an element from another view, it gets inserted as QTreeWidgetItem. I'd like it to be inserted as QTreeWidgetCustomItem. Is this possible?
For now, I'm gonna do it by replacing it as soon as it gets inserted but if there's a cleaner way, that would be better...

tbscope
6th June 2010, 18:11
Learn about casting. Try a qobject_cast for example

Olivier Berten
6th June 2010, 22:42
Didn't get very far in that casting thing but it seems it doesn't fit my case:
- I'm using Python
- QTreeWidgetItem doesn't inherit QObject

SixDegrees
6th June 2010, 23:35
Your table is already storing your CustomItem as a CustomItem; it doesn't get converted to an Item, that's just the OO approach for handling inheritance. The (bad) alternative would be to require everyone to also reimplement Table to accept customized Items. Not a good plan.

IN C++, you simply cast your Item pointer to a CustomItem, checking to make sure the cast actually worked; it is, after all, already a CustomItem, you just have to tell the runtime to treat it as such. How to cast from base to derived class in Python is left as an exercise, particularly since I'm not a Python programmer. It is certainly possible, however.

Olivier Berten
7th June 2010, 00:23
Well, let's give some more details as it doesn't seem to be very clear.

I have a QTreeWidget and a QListWidget.

In the QTreeWidget I have items of 4 different subclasses of QTreeWidgetItem. Clicking on items fires actions depending of the item type.

What I have now: drag-n-dropping from the QListWidget creates generic QTreeWidgetItem which my program doesn't know what to do with...

What I want to achieve: drag-n-dropping from the QListWidget creates items of one specific type in the QTreeWidget.

SixDegrees
7th June 2010, 00:59
See my previous reply; the items aren't "created" all over again as generic items. They're the same CustomItems you handed the tree to begin with. You have to cast the tree's base pointer to the type of your derived class in order to access its particular features, but they're still there.

Python allows you to do the same thing. Read up on casting, derived classes, base classes and type promotion.

Olivier Berten
7th June 2010, 08:58
See my previous reply; the items aren't "created" all over again as generic items. They're the same CustomItems you handed the tree to begin with. You have to cast the tree's base pointer to the type of your derived class in order to access its particular features, but they're still there.

Well... I must be misunderstanding something...

Here are the existing tree items

<__main__.treeItemSwatch object at 0xa591bac>
<__main__.treeItemSwatch object at 0xa591eac>
<__main__.treeItemGroup object at 0xa64fe6c>
<__main__.treeItemSpacer object at 0xa64feac>
<__main__.treeItemBreak object at 0xa64f06c>
<__main__.treeItemSwatch object at 0xa64f0ec>
And here are the dropped items...

<PyQt4.QtGui.QTreeWidgetItem object at 0xa659bac>
<PyQt4.QtGui.QTreeWidgetItem object at 0xa655d2c>
<PyQt4.QtGui.QTreeWidgetItem object at 0xa655e2c>
<PyQt4.QtGui.QTreeWidgetItem object at 0xa65932c>

How are these supposed to be "the same CustomItems"? How Qt would guess which of my 4 custom types I want it to be?


Python allows you to do the same thing. Read up on casting, derived classes, base classes and type promotion.
Could you point me to some references? As far as I've searched now, I read type casting doesn't exist in Python...

tbscope
7th June 2010, 09:18
A TreeItemSwatch for example is based on a TreeWidgetItem? If so, the base class is a TreeWidgetItem. Since a TreeWidget can't possibly know all custom items, it uses the base class. It's up to you to tell it that it is actually a specialised version, and you do this with casting for example. How this is done in Python, I don't know. Did you try TreeItemSwatch(TreeWidgetItem) ?

SixDegrees
7th June 2010, 09:24
I don't know how Python does it, as already mentioned. I know that it does, however; this is possibly the most common feature of all OO languages. It would be impossible to do run time type evaluation without it, and inheritance would lose most of its value.

I would imagine this would be treated in any Python reference early on.

SixDegrees
7th June 2010, 09:27
How Qt would guess which of my 4 custom types I want it to be?

It can't - that's the whole point. C++ (not Qt) also cannot know what sort of derived class you're going to drop into the tree, so it stores all derived classes as a pointer to base object. It's YOUR responsibility to know what's in there and cast it to the correct type, or use whatever idiom for doing something similar is available in Python.