如何将多个项目设置为TreeView拖放的GtkSelection [英] How to set multiple items into a GtkSelection for Treeview drag and drop

查看:160
本文介绍了如何将多个项目设置为TreeView拖放的GtkSelection的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我当前的项目使用一个 Gtk.TreeView 显示每行四个字段的 ListView 的内容,两个字符串,一个 int 和一个布尔值。我试图在TreeView中实现拖放行的重排。我不想简单地为内置的拖放使用 TreeView.set_reorderable(True),因为我想对数据的插入和删除进行一些控制该模型以及能够实现拖放操作的撤销/重做。我正在使用Python 3.2和PyGObject 3。

My current project uses a Gtk.TreeView to display the contents of a ListView with four fields per row, two strings, an int and a boolean. I'm trying to implement drag and drop rearrangement of rows in the TreeView. I don't want simply to use TreeView.set_reorderable(True) for the built-in drag and drop because I want to have some control over the insertion and deletion of data from the model as well as to be able to implement undo/redo of drag and drop operations. I'm using Python 3.2 and PyGObject 3.

我现在遇到的问题是弄清楚如何在我的 drag_data_get 方法使用两个字符串设置选择数据对象,一个int和一个 bool 组成要拖放的行。我已经能够找到的所有示例代码都涉及到具有单列列的树视图,字符串值将被设置为这样的选择:

The problem I'm now having is figuring out how in my drag_data_get method to set the selection data object with the two strings, one int and one bool that make up the row to be dragged and dropped. All the example code I've been able to find involves treeviews with a single column with string values that get set into the selection with something like this:

def drag_data_get_data(self, treeview, context, selection, target_id, etime):
    treeselection = treeview.get_selection()
    model, iter = treeselection.get_selected()
    data = bytes(model.get_value(iter, 0), "utf-8")
    selection.set(selection.get_target(), 8, data)

我所有努力设置选择对象与我的一个TreeView行中的数据失败。我的模型中的 int bool 值不能像字符串值那样编码,我找不到任何示例如何将多列TreeView行的所有值设置为单个选择对象。任何人都可以指出一些相关的示例或文档?

All my efforts to set the selection object with the data from one of my TreeView rows have failed. The int and bool values in my model can't be encoded like string values and I can't find any examples of how to set all the values for a multi-column TreeView row into a single selection object. Can anyone point me to some relevant examples or docs?

推荐答案

您可以将4个值的元组编码为单个字符串。一个简单的方法是使用 json

You could encode your tuple of 4 values into a single string. An easy way is to use json for that:

import json
data = ["string", "string2", True, 20]
string_variable = json.dumps(data)
#
# now pass string_variable through drag and drop
#
returned = json.loads(string_variable)

您还可以使用自己的编码导入 json 的方案不适合您。

You could also use your own encoding scheme if importing json is not an option for you.

请仔细检查您获得的数据这条路。如果你没有,一些特制的字符串(从另一个程序传递)可能会让你的程序崩溃或更糟。

Please do a careful sanity check on the data you get this way. If you don't, some specially crafted string (passed from another program, say) might crash you program or worse.

这篇关于如何将多个项目设置为TreeView拖放的GtkSelection的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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