QMimeData中的Python对象 [英] Python object in QMimeData

查看:288
本文介绍了QMimeData中的Python对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在根据我的自定义模型实现拖放QTreeView。一切工作正常,我的树显示数据,拖放已启用,现在最后一步在我之前 - 删除和拖动拖动的数据。为此,我需要在我的模型中实现mimeTypes,mimeData和dropMimeData方法。现在我的问题是:有没有任何简单的标准方式如何通过QMimeData传递任意Python对象?我正在QTreeView内部做一个内部动作,显示我的Python类Person的层次结构。而且我想重新排序。没有拖放到应用程序之外,甚至不能控制。我发现只有一个教程:链接文本。但这是唯一的办法吗?不能将Python对象编码到ByteArray中。我只需要一个班级的人才需要很简单的解决方案。谢谢。

解决方案

不要尝试通过重新定位底层的python对象来实现拖放。如果拖动来自您的进程外部,这将不起作用;也不会为复制操作(您的节点对象可能不存在于树中的多个位置)。



想想将移动拖放到三个操作:


  1. 将数据序列化为某个字节串

  2. 反序列化为新索引(或新索引)

  3. (可选:如果移动而不是复制)删除旧的索引

mineData()和dropMimeData()是您提供的序列化和反序列化操作。 Python提供了一些简单的方法来实现它们 - 检查pickle模块的文档。如果你很幸运,那么pickle.dumps()和pickle.loads()将为你开箱即用。



编辑:我不能弄清楚如何在代码中粘贴代码,所以这里是我的评论引用的解决方案。这是安全的,在某种意义上说,如果碰巧违反规则,它会失败,而不是造成崩溃。

 #来检索数据。 
self .__ tmp_storage_dct = {self .__ tmp_storage_cooky:stuff}
m.setData(self.rowlistptr_mime_type,QByteArray(pickle.dumps(self .__ tmp_storage_cooky)))
self .__ tmp_storage_cooky + = 1

#drop:
如果mime.hasFormat(self.rowlistptr_mime_type):
打印got tmpstorage
cooky = pickle.loads(mime.data(self.rowlistptr_mime_type ).data())
nodes = self .__ tmp_storage_dct.pop(cooky)


I'm implementing drag and drop QTreeView based on my custom model. All works fine, my tree displays data, drag and drop is enabled and now the last step lies ahead of me - to drop and trasfer dragged data. To do this I need to implement mimeTypes, mimeData and dropMimeData methods in my model. And now my question: Is there any easy standard way how to pass an arbitrary Python object through QMimeData? I'm doing just an internal move within QTreeView which displays hierarchy of my Python classes Person. And I want to reorder them. No drag and drop outside the application, not even outside of control. I have found only single one tutorial: link text. But is it the only way? Cannot it be done without encoding the Python object into ByteArray. I need really simple solution for my only one class Person. Thank you.

解决方案

Do not try to implement drag and drop by reparenting the underlying python object. This won't work if the drag comes from outside your process; nor will it work for a copy operation (your node objects probably cannot exist in multiple places in the tree).

Think of a drag and drop "move" as three operations:

  1. serialize the data to some byte string
  2. deserialize into a new index (or new indexes)
  3. (optional: if "move" rather than "copy") remove the old index(es)

mineData() and dropMimeData() are the serialize and deserialize operations that you provide. Python provides some easy ways to implement them -- check the documentation for the pickle module. If you're lucky, pickle.dumps() and pickle.loads() will work out-of-the-box for you.

Edit: I couldn't figure out how to paste code in comments, so here's the solution my comment refers to. This is safe, in the sense that it will fail by throwing a KeyError instead of causing crashes if you happen to break your rules.

# drag: store off the data in a safe place, and serialize a cooky
# that the drop target can use to retrieve the data.
self.__tmp_storage_dct = { self.__tmp_storage_cooky: stuff }
m.setData(self.rowlistptr_mime_type, QByteArray(pickle.dumps(self.__tmp_storage_cooky)))
self.__tmp_storage_cooky += 1

# drop:
if mime.hasFormat(self.rowlistptr_mime_type):
  print "got tmpstorage"
  cooky = pickle.loads(mime.data(self.rowlistptr_mime_type).data())
  nodes = self.__tmp_storage_dct.pop(cooky)

这篇关于QMimeData中的Python对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆