3

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?

BobMorane
  • 3,870
  • 3
  • 20
  • 42
  • With regards to this: "I'm also new to gtkmm", see my answer [here](https://stackoverflow.com/questions/67248858/where-can-i-find-good-documentation-for-gtkmm/67248860#67248860) for documentation for Gtkmm around the internet. – BobMorane Aug 07 '21 at 17:11

1 Answers1

1

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();
}
BobMorane
  • 3,870
  • 3
  • 20
  • 42
  • 1
    It worked! (you missed "," after "*this") The answer is so obvious after you see it... – Vulpes-Vulpeos Aug 07 '21 at 17:10
  • yesterday I was so happy to see native file chooser dialog that completely forgot about second part - printing path to the chosen folder into terminal. I tried to solve this myself but got stuck again :( If I use code from example https://developer-old.gnome.org/gtkmm-tutorial/4.0/gtkmm-tutorial.html#sec-dialogs-filechooserdialog) `dialog->signal_response().connect(sigc::bind(sigc::mem_fun(*this, &MyWindow::on_folder_dialog_response), dialog));` I get a lot of "in instantiation of function template specialization" errors in gtkmm4 files. – Vulpes-Vulpeos Aug 08 '21 at 05:26
  • Here is the [build log](https://pastebin.com/9ppRrGLT) , here is [the code of app](https://pastebin.com/DMTVFK2p) I try to build. – Vulpes-Vulpeos Aug 08 '21 at 07:11
  • I think it would be better if you asked a separate question for this, adding a minimal and reproducible example to demonstrate your problem (you can include errors). This question seemed to be about not being able to construct a file chooser object, better not mix. If you create another question, I'll be happy to take a look at it! – BobMorane Aug 08 '21 at 13:00