QMimeData中的Python对象 [英] Python object in QMimeData
问题描述
不要尝试通过重新定位底层的python对象来实现拖放。如果拖动来自您的进程外部,这将不起作用;也不会为复制操作(您的节点对象可能不存在于树中的多个位置)。
想想将移动拖放到三个操作:
- 将数据序列化为某个字节串
- 反序列化为新索引(或新索引)
- (可选:如果移动而不是复制)删除旧的索引
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:
- serialize the data to some byte string
- deserialize into a new index (or new indexes)
- (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屋!