I am trying to open a Gtkmm::FileChooserDialog
to choose multiple files and print their paths along with filenames to a label. I can open the dialog and choose the files but I am having a hard time reading the filenames to my variables.
FileChooserDialog openFileDialog("", FILE_CHOOSER_ACTION_OPEN);
openFileDialog.add_button("Cancel", RESPONSE_CANCEL);
openFileDialog.add_button("Open", RESPONSE_OK);
openFileDialog.set_current_folder(ustring::compose("%1/Desktop", ustring(getenv("HOME"))));
openFileDialog.set_transient_for(*this);
openFileDialog.set_select_multiple(true);
Glib::RefPtr<Gtk::FileFilter> fileFilter = Gtk::FileFilter::create();
fileFilter->set_name("Text Files (*.txt)");
fileFilter->add_pattern("*.txt");
openFileDialog.add_filter(fileFilter);
fileFilter = Gtk::FileFilter::create();
fileFilter->set_name("All Files (*.*)");
fileFilter->add_pattern("*.*");
openFileDialog.add_filter(fileFilter);
if (openFileDialog.run() == RESPONSE_OK)
label.set_text(ustring::compose("File = %1", ustring(openFileDialog.get_filename())));
return true;
You can use Gtk::FileChooser::get_filenames
(Gtkmm 3.24):
if (openFileDialog.run() == Gtk::RESPONSE_OK)
{
for(const auto& fileName : openFileDialog.get_filenames())
{
label.set_text(Glib::ustring::compose("File = %1", Glib::ustring(fileName)));
}
}
which returns a std::vector
of file names.
Note: In my answer, I keep overwriting the label
variable because it was all the context there was in your code snippet. My be you have multiple labels or you want to pack all the file names somehow in a single label. I let this part to you.