Search code examples
rustgtk-rs

How do I add action buttons to a FileChooserDialog?


When I try to show a file chooser dialog, it is missing the action buttons:

let dialog = FileChooserDialog::new(Some("Open File"), Some(&window), FileChooserAction::Open);
dialog.run();

missing action buttons

I found another way from another project:

let dialog = FileChooserDialog::new_with_buttons::<ApplicationWindow>(
    Some("Open File"),
    Some(&window),
    FileChooserAction::Open,
    &[
        ("_Cancel", ResponseType::Cancel),
        ("_Open", ResponseType::Accept),
    ],
);

The error message is:

no function or associated item named `new_with_buttons` found for type `gtk::FileChooserDialog` in the current scope

Solution

  • I guess you need to use add_button to add the buttons after creating the dialog and before showing it:

    let dialog = FileChooserDialog::new(Some("Open File"), Some(&window), FileChooserAction::Open);
    dialog.add_button("_Cancel", ResponseType::Cancel);
    dialog.add_button("_Open", ResponseType::Accept);
    dialog.run();