Help with drag and drop of files into other Gnome apps

Hi! I’m trying to program drag and drop of files from my Gtk4 app into other Gnome apps - particularily nautilus.
The code I am using is along the lines of:

def on_setup(factory, listitem):
    widget = ...
    listitem.set_child(widget)
    ev_drag = Gtk.DragSource(actions=Gdk.DragAction.COPY)
    ev_drag.connect('prepare', on_drag_prepare)
    widget.add_controller(ev_drag)

def on_bind(factory, listitem):
    widget = listitem.get_child()
    item = listitem.get_item()
    widget.item = item   # to find the item from the widget

def on_drag_prepare(event, x, y):
    widget = event.get_widget()
    item = widget.item
    gfile = ... # get the Gio.File from 'item' here
    content = Gdk.FileList.new_from_list([gfile])
    return Gdk.ContentProvider.new_for_value(content)

Wierdly enough, drag and drop works with Visual Studio Code and within the app but with no other apps.

Any thoughts on how to make this work? I am using Python but tips in any other programming languages are welcome.

Hi,

Have you tried dropping files from other apps to Nautilus? If yes, does it work, or always fail?

If you can make it work with other apps, it could be interesting to start the inspector (press CtrlShiftI from Nautilus), select the “Global” tab and the “Clipboard” section, there is a drop zone at the top where you can inspect and compare the dropped data.

1 Like

Also refer following docs on DND:

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