c++gtkmmgtkmm4

Example of gtkmm 4 FileChooserNative code


I can't find code examples using Gtk::FileChooserNative to help me understand how to work with this class. Documentation from here isn't that helpful.

My goal is to create a function which opens a native file chooser dialog and after the user selects the folder, prints the path to the folder into a terminal.

When I try to compile this:

void MyWindow::on_button_browse_clicked()
{
    Gtk::FileChooserNative dialog ("Please choose a folder", 
                                   Gtk::FileChooser::Action::SELECT_FOLDER,
                                   "Choose",
                                   "Cancel");
}

I get the following error:

error: calling a protected constructor of class 'Gtk::FileChooserNative'

How can I create a Gtk::FileChooserNative?


Solution

  • I don't have Gtkmm 4 here, but from the documentation you posted, it seems you need to use a factory method instead of a constructor to create such a dialog:

    static Glib::RefPtr<FileChooserNative> Gtk::FileChooserNative::create(
            const Glib::ustring& title,
            Window&              parent,
            FileChooser::Action  action,
            const Glib::ustring& accept_label = {},
            const Glib::ustring& cancel_label = {} 
    )
    

    In your case, something like:

    void MyWindow::on_button_browse_clicked()
    {
        auto dialog = Gtk::FileChooserNative::create("Please choose a folder",  
                                                     *this,    
                                                     Gtk::FileChooser::Action::SELECT_FOLDER ,
                                                     "Choose",
                                                     "Cancel");
    
        dialog->show();
    }