0

我正在测试一个 gtk4 小部件,它是 GtkDropTarget。我计划将拖到窗口的图像设置为窗口本身的图像。但是,一旦我拖动图像文件,就会出现错误。需要明确的是,这是 vala 中的代码:

int main (string[] args) {
    var app = new App();
    return app.run(args);
}

public class App : Gtk.Application {
    public App () {
    Object (
        application_id: "com.github.ea",
        flags: ApplicationFlags.FLAGS_NONE
    );
    }
    public override void activate () {
    var window = new Window (this);
    add_window (window);
    }
}

public class Window : Gtk.ApplicationWindow {
    public Window (Gtk.Application app) {
        Object (application: app);
    }

construct {
        title = "Drag";
    set_default_size (640, 480);
    var drag_source = new DragSource ();
    set_child (drag_source.self);       
    show ();
    }
}

public class DragSource {
    public Gtk.Image self;

    public DragSource () {

        self = new Gtk.Image ();
        var drag_controller = new Gtk.DropTarget (GLib.Type.INVALID,     Gdk.DragAction.COPY);

        drag_controller.set_gtypes ({typeof(File)});

        self.add_controller (drag_controller);
        drag_controller.on_drop.connect (on_drop);
    }

    private bool on_drop (GLib.Value val, double x, double y) {
        File filename = (File) val;
        var file_path = filename.get_path ();
        
        if (val.holds(typeof(File)) == true) {
        print ("The dragged object is a file.\n");

        if ("png" in file_path || "jpg" in file_path) {
                print ("The dragged object is an image.\n");
                self.set_from_pixbuf (pixbuf(file_path));
        }
            else {
                print ("The dragged object is NOT an image.\n");
        }
    }
        else {
            print ("The dragged object is NOT a file.\n");
        return false;
        }
        return true;
    }

    private Gdk.Pixbuf pixbuf (string file) {
        try {
            return new Gdk.Pixbuf.from_file (file);
        } catch (Error e) {
            error ("%s", e.message);
            }
        }
}

这将编译并运行。但是一旦我将图像文件拖到窗口中,就会发生错误并且图像不显示。这是发生的事情的图片。应该发生的是,当我从文件管理器中拖动 png 文件时,拖动的图像应该是 GtkImage 中显示的图像,它是窗口的主要小部件。

在我第一次从我的电脑拖动图像文件时,出现了这个错误:

The dragged object is a file.
The dragged object is an image.

(v:3891): Gtk-CRITICAL **: 08:52:28.861: gtk_image_set_from_pixbuf: assertion 'GTK_IS_IMAGE (image)' failed

在第二次拖动时,显示如下:

(v:3891): Gdk-CRITICAL **: 08:53:33.388: gdk_drop_set_actions: assertion 'priv->state == GDK_DROP_STATE_NONE' failed
The dragged object is a file.
The dragged object is an image.

(v:3891): Gtk-CRITICAL **: 08:53:33.973: gtk_image_set_from_pixbuf: assertion 'GTK_IS_IMAGE (image)' failed

我真的很感激帮助。谢谢你!

4

1 回答 1

1

这就是我将如何实现你的意图

int main (string[] args) {
    var app = new App ();
    return app.run (args);
}

public class App : Gtk.Application {
    public App () {
        Object (
            application_id: "com.github.ea",
            flags : ApplicationFlags.FLAGS_NONE
        );
    }

    public override void activate () {
        var window = new Window (this);
        window.present ();
    }
}

public class Window : Gtk.ApplicationWindow {
    public Window (Gtk.Application app) {
        Object (application: app);
    }

    construct {
        title = "Drag an Image!";
        set_default_size (640, 480);

        var image = new Gtk.Image ();
        image.vexpand = image.hexpand = true;

        var controller = new Gtk.DropTarget (typeof (GLib.File), Gdk.DragAction.COPY);
        controller.on_drop.connect ((target, value, x, y) => {
            var file = (GLib.File) value;
            var filename = file.get_path ();
            if (GLib.ContentType.guess (filename, null, null).contains ("image")) {
                image.set_from_file (filename);
            }
        });
        image.add_controller (controller);

        set_child (image);
    }
}
于 2021-08-10T13:35:08.553 回答