Hi,
I want to use drag&drop in combination with wx.dataview.DataViewCtrl
, so I started with trying to reorder rows using Drag&Drop. I was unable to find any examples how to do this correctly in wxpython, but anyhow, I managed to get it partially working (see below).
Unfortunately, event.GetDataObject()
always returns None
in my on_drop
function. Any pointers how to get the DataObject I assigned in on_begin_drag
? What am I doing wrong?
I already asked in the #wxpython IRC channel and on stackoverflow, but to no avail.
Thanks in advance!
My Setup:
- ArchLinux (64-Bit)
- python2 2.7.9-1
- wxpython 3.0.2.0-1
- gtk3 3.14.8-1
- gnome-shell 3.14.3-2
Example Code:
import wx
import wx.
dataview
DF_PLAYLIST_SONG = wx.CustomDataFormat("playlist_song")
class MyDataViewCtrl(wx.dataview.DataViewCtrl)
def __init__(self, *args. **kwargs)
[...]
self.Bind(wx.dataview.EVT_DATAVIEW_ITEM_BEGIN_DRAG, self.on_begin_drag)
self.Bind(wx.dataview.EVT_DATAVIEW_ITEM_DROP, self.on_drop)
self.EnableDragSource(DF_PLAYLIST_SONG)
self.EnableDropTarget(DF_PLAYLIST_SONG)
[...]
def on_begin_drag(self, event):
text = self._model.GetValue(event.GetItem(), 0)
data = wx.CustomDataObject(DF_PLAYLIST_SONG)
# Need to encode, because SetData dislikes unicode
data.SetData(text.encode('utf-8'))
event.SetDataObject(data)
#data.this.disown() # Makes no difference if uncommented or not
def on_drop(self, event):
print(event.GetDataFormat()) # Returns DF_PLAYLIST_SONG
if event.GetDataFormat() == DF_PLAYLIST_SONG:
# This would be logical choice:
print(event.GetDataSize()) # Returns the size of the data, e.g 92
print(event.GetDataObject()) # Returns None (strange!)
# Some other stuff I tried
print(event.GetClientObject()) # Returns MyDataViewCtrl instance
print(event.GetEventObject()) # Returns None
print(event.GetValue()) # Returns <Swig Object of type 'wxVariant *' at 0x7fffa340a0d0>
print(self._model.GetValue(event.GetItem(), 0)) # Returns column 0 of the row this was dropped on
print(event.GetItem()) # Returns the wx.dataview.DataViewItem this was dropped on
print(event.GetDataBuffer()) # Returns <Swig Object of type 'void *' at 0x1a59b30>