J Ellianef wrote:
Hello,
Thanks for your previous explanations.
Now I 'm trying to implement drag and drop and have problems with TreeItemId. If a tree is displayed and I begin dragging an item, it's ID is not always the same.
Code:
def OnBeginDrag(self, event):
self.drag_src_item=event.GetItem()
wx.TreeEvent.Allow(event)
print "Drag Src item=",self.drag_src_item
self.drag_src_data= self.GetPyData( self.drag_src_item)
and dragging 6 times the same item I get:
Drag Src item= <wx._controls.TreeItemId; proxy of C++ wxTreeItemId instance at _78a86f01_p_wxTreeItemId>
Drag Src item= <wx._controls.TreeItemId; proxy of C++ wxTreeItemId instance at _e8a86f01_p_wxTreeItemId>
Drag Src item= <wx._controls.TreeItemId; proxy of C++ wxTreeItemId instance at _d0a66f01_p_wxTreeItemId>
Drag Src item= <wx._controls.TreeItemId; proxy of C++ wxTreeItemId instance at _78a86f01_p_wxTreeItemId>
Drag Src item= <wx._controls.TreeItemId; proxy of C++ wxTreeItemId instance at _e8a86f01_p_wxTreeItemId>
Drag Src item= <wx._controls.TreeItemId; proxy of C++ wxTreeItemId instance at _d0a66f01_p_wxTreeItemId>
The instance is quite random, and is ennoying for collapseAndReset and Expand the parent to update the tree view after drag-drop.
The C++ wxTreeItemId is just an "opaque" data structure that abstracts the concept of a handle to an item in the tree, so it's the hidden guts of the wxTreeItemId that matter, not the wxTreeItemId itself. (In fact since it is such a cheap operation the guts are copied to new instances often, such as every time one is returned from a method, etc. That allows the internal code to be simpler and not have to worry about dynamic allocation, ref-counting, etc. )
The Python wrapper for wxTreeItemId is just a proxy for the C++ class, and again, new instances of the proxy may be created as needed, even for the same instance of the the C++ object. However there is an equality operator that should tell you if the two items refer to the same node in the tree, for example:
if currentItem == self.oldItem:
...
Alternately, is it possible to retrieve an ItemId from it's data since the PyData is always correct?
In most cases you should assume that the wx.TreeItemId objects are transitory, IOW they can come and go as needed. The main exception to this is the root node, which, unless you remove and add a new root, will always contain the handle for the root. If you need to have some sort of unique ID for the items then put it in your PyData, but I don't think I would store the wx.TreeItemId there because the internal handles could possibly change as other items are added or deleted. Instead you could just use the wx.TreeItemId's to respond to tree events, navigate to parent, child or sibling nodes, etc., and then use the attached PyData for everything else.
···
--
Robin Dunn
Software Craftsman
http://wxPython.org Java give you jitters? Relax with wxPython!