如何使用wxpython的DataViewCtrl拖放

时间:2015-02-24 13:44:09

标签: python python-2.7 drag-and-drop wxpython wxwidgets

我想与wx.dataview.DataViewCtrl结合使用拖放功能,因此我开始尝试使用Drag& Drop重新排序行。我无法在wxpython中找到任何如何正确执行此操作的示例,但无论如何,我设法让它部分工作(见下文)。

不幸的是,event.GetDataObject()总是在None函数中返回on_drop。有关如何获取on_begin_drag中指定的DataObject的任何指针?我做错了什么?

提前致谢!

设定:

  • ArchLinux(64位)
  • python2 2.7.9-1
  • wxpython 3.0.2.0-1
  • gtk3 3.14.8-1
  • gnome-shell 3.14.3-2

示例代码:

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>

2 个答案:

答案 0 :(得分:1)

你没有在wxEVT_DATAVIEW_ITEM_DROP hander中找回数据对象,它只用于从控件中拖出数据。删除时,您会获得原始数据及其格式,即您应使用GetDataSize()GetDataBuffer()来访问它。

答案 1 :(得分:0)

this example on a wxWidgets/wxPython ticket 15100。我使用它来为wx.DataViewCtrl进行拖放,并发生树形数据结构(以获取对象)。至少在2.9.5 / msw上工作。 HitTest已在3.0.2中实现(如果您想知道哪个项目被命中)。

可能不是您问题的正确答案,但至少它确实有效(请参阅对象项目的疯狂腌制)。