user1608721
user1608721

Reputation: 158

GTK4 Vala - show FileChooserDialog

I am playing around with Vala and GTK4.

FileChooserDialog is not working for me

    using Gtk;
    
    int main (string[] argv) {
        // Create a new application
        var app = new Gtk.Application ("com.example.GtkApplication",
                                       GLib.ApplicationFlags.FLAGS_NONE);
    
        app.activate.connect (() => {
            // Create a new window
            var window = new Gtk.ApplicationWindow (app);
    
    
            window.title = "File chooser";
            window.set_default_size (350, 70);
            window.resizable = false;
    
    
        // Create a new button
        var file_choose_button = new Gtk.Button.with_label ("...");
        file_choose_button.clicked.connect (() => {
            var fileChooser = new FileChooserDialog(
                "Select File",
                window,
                FileChooserAction.OPEN,
                "Cancel",
                ResponseType.CANCEL,
                "Open",
                ResponseType.ACCEPT,
                null);
            fileChooser.response.connect(()=> {
                stdout.printf("File selectd!");
    
            });
            // WHAT TO DO IN ORDER TO SHOW FILE CHOOSER?
        });
        window.set_child (file_choose_button);
        // Show
        window.present ();
    });

    return app.run (argv);
 }

I am missing some important piece of code, that will cause the FileChooserDialog to "appear".

In previous Versions of GTK there is "dialog.run" - which is missing in GTK4.

The C-Example on https://docs.gtk.org/gtk4/class.FileChooserDialog.html uses makro(?) "gtk_widget_show(xxx)" for which I was not able to find an representation in Vala.

Any help appreciated!

Best Regards

Emil

Upvotes: 1

Views: 576

Answers (1)

user1608721
user1608721

Reputation: 158

After some struggle the solution was found (and is pretty simple).

As stated in the Vala Documentaion Site - File Chooser Dialog It inherits from couple of classes one of which is GTK.Window.

So it is as simple as calling the present() method.

Thus the missing command above is:

fileChooser.present();

One should not forget to use the close() method once file was selected or selection was canceled.

Important note: "gtk_widget_show()" representation in Vala is GTK.Widget.show() BUT I was not clever enough to find out how to prepare the parameter. It expects pointer (GtkWidget*) and simply passing the "fileChooser" causes all kinds of compiler exceptions.

May be someone can throw more light on this (as I am using Vala to avoid the use of C - I am clearly not the expert in this area)

Upvotes: 0

Related Questions