Specifying custom cargo for drag and drop

I was proud of myself for figuring out how to specify cargo for drag and drop between two treeviews considering how scant the documentation is. My code works fine, but there is one detail about which I am uncertain. Here are some code fragments that show my solution:

# Enable drag and drop from source treeview.
source_treeview.enable_model_drag_source(
        Gdk.ModifierType.BUTTON1_MASK,
        [Gtk.TargetEntry.new('name', Gtk.TargetFlags.SAME_APP, 0)],
        Gdk.DragAction.COPY)
source_treeview.connect('drag-data-get', self.on_drag_data_get)

def on_drag_data_get(self, treeview, context, data, info, time):
    drag_cargo = pickle.dumps((data1, data2, data3))
    data.set(data.get_selection(), 8, drag_cargo)


# Enable drag and drop to target treeview.
target_treeview.enable_model_drag_dest(
        [Gtk.TargetEntry.new('name', Gtk.TargetFlags.SAME_APP, 0)],
        Gdk.DragAction.COPY)

target_treeview.connect('drag-data-received',
        self.on_target_treeview_drag_data_received)

def on_target_treeview_drag_data_received(self, treeview, context,
        x, y, data, info, time):
    data1, data2, data3 = pickle.loads(data.get_data())

The keys to the solution are the data.set and data.get_data calls and also the use of pickle to serialize my data to bytes. This code works, but I never fully understood the first argument to data.set. It is supposed to be a Gdk.Atom that specifies the “type of selection data”. get_selection returns “the selection Gdk.Atom of the selection data”. I interpreted the first part of the phrase to mean the type, so I think I have this right. However, it also works to use data.get_target, which might also make sense because the type of the target is probably always the same as the type of the source. I would appreciate any confirmation, elucidation, or criticism of this solution.

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.