在Gtk.Treeview中设置我自己的拖动图标

问题描述 投票:1回答:1

我想在Gtk.Treeview中使用我自己的拖动图标。 official tutorialdrag-begin信号/事件可以用于此。 API-Reference为more details提供了有关如何操作的信息。

但在我的例子中,它不起作用。仍然使用树视图默认拖动图标。如果我通过connect()connect_after()连接并不重要。

#!/usr/bin/env python3
import gi
gi.require_version('Gtk', '3.0')
from gi.repository import Gtk
from gi.repository import Gdk
from gi.repository import GdkPixbuf

# from wikipedia
my_xpm = [
"24 20 3 1 12 10 XPMEXT",
"  c None",
". c #0000FF",
"+ c #FF0000",
"                        ",
"    ..                  ",
"   ....                 ",
"  ......++++++++        ",
" .........+++++++       ",
" ..........+++++++      ",
" ............++++++     ",
" .............++++++    ",
"  ..............++++    ",
"   +.............+++    ",
"   ++.............++    ",
"   +++.............+    ",
"   +++++.............   ",
"   ++++++.............. ",
"   ++++++++............ ",
"   +++++++++........... ",
"    +++++++++.........  ",
"     ++++++++++.......  ",
"      ++++++++++.....   ",
"       +++++++++ ...    "
]

class MainWindow(Gtk.Window):
    def __init__(self):
        Gtk.Window.__init__(self, title="TreeView Drag and Drop")
        self.connect("delete-event", Gtk.main_quit)
        self.box = Gtk.Box()
        self.add(self.box)

        # "model" with dummy data
        self.store = Gtk.TreeStore(str)
        for i in range(5):
            self.store.append(None, ['Item {}'.format(i)]) # treeview
        self.tree = Gtk.TreeView(model=self.store)
        self.box.pack_start(self.tree, True, True, 0)

        # build columsn
        colA = Gtk.TreeViewColumn('Col A', Gtk.CellRendererText(), text=0)
        self.tree.append_column(colA)

        # icon view
        self._icon = GdkPixbuf.Pixbuf.new_from_xpm_data(my_xpm)
        self.image = Gtk.Image.new_from_pixbuf(self._icon)
        self.box.pack_start(self.image, True, True, 0)

        # DnD events
        self.tree.connect("drag-data-received", self.drag_data_received)
        self.tree.connect("drag-data-get", self.drag_data_get)
        #self.tree.connect("drag-begin", self.drag_begin)
        self.tree.connect_after("drag-begin", self.drag_begin)

        target_entry = Gtk.TargetEntry.new('text/plain', 2, 0)
        self.tree.enable_model_drag_source(
                Gdk.ModifierType.BUTTON1_MASK,[target_entry], 
                Gdk.DragAction.DEFAULT|Gdk.DragAction.MOVE
        )
        self.tree.enable_model_drag_dest(
                [target_entry],
                Gdk.DragAction.DEFAULT|Gdk.DragAction.MOVE
        )

    def drag_data_get (self, treeview, drag_context, data, info, time):
        model, path = treeview.get_selection().get_selected_rows()
        print('dd-get\tpath: {}'.format(path))
        data.set_text(str(path[0]), -1)

    def drag_data_received (self, treeview, drag_context, x,y, data,info, time):
        print('dd-received')
        store = treeview.get_model()
        source_iter = store.get_iter(data.get_text())
        dest_path, drop_pos = self.tree.get_dest_row_at_pos(x, y)
        print('path: {} pos: {}'.format(dest_path, drop_pos))

    def drag_begin(self, widget, context):
        self._drag_icon = GdkPixbuf.Pixbuf.new_from_xpm_data(my_xpm)
        widget.drag_source_set_icon_pixbuf(self._drag_icon)
        return context

win = MainWindow()
win.show_all()
Gtk.main()
python python-3.x gtk3 pygobject
1个回答
0
投票

这个答案是基于GitLab Issue

关于这一点,文档不清楚。 drag_begin()应该是这样的

def drag_begin(self, widget, context):
    self._drag_icon = GdkPixbuf.Pixbuf.new_from_xpm_data(my_xpm)
    Gtk.drag_set_icon_pixbuf(context, self._drag_icon, 0, 0)
© www.soinside.com 2019 - 2024. All rights reserved.