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?
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?
You could derive your QTreeWidgetCustomItem from QTreeWidgetItem.
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...
Learn about casting. Try a qobject_cast for example
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
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.
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.
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.
Well... I must be misunderstanding something...
Here are the existing tree items
And here are the dropped items...Qt Code:
<__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>To copy to clipboard, switch view to plain text mode
Qt Code:
<PyQt4.QtGui.QTreeWidgetItem object at 0xa659bac> <PyQt4.QtGui.QTreeWidgetItem object at 0xa655d2c> <PyQt4.QtGui.QTreeWidgetItem object at 0xa655e2c> <PyQt4.QtGui.QTreeWidgetItem object at 0xa65932c>To copy to clipboard, switch view to plain text mode
How are these supposed to be "the same CustomItems"? How Qt would guess which of my 4 custom types I want it to be?
Could you point me to some references? As far as I've searched now, I read type casting doesn't exist in Python...
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) ?
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.
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.How Qt would guess which of my 4 custom types I want it to be?
Bookmarks