Gtk4 C - get file name from file chooser


please could any body helps me how to get name (with full path) choosed with file chooser.

when I select file and press button open. I receive error (and app crash):

(dialog_soubor.exe:3504): Gtk-CRITICAL **: 12:55:44.880: gtk_file_chooser_widget_get_current_name: assertion 'impl->action == GTK_FILE_CHOOSER_ACTION_SAVE' failed
terminate called after throwing an instance of 'std::logic_error'
  what():  basic_string::_M_construct null not valid

Process returned 3 (0x3)   execution time : 10.002 s
Press any key to continue.

my code is:

#include <gtk/gtk.h>
#include <iostream>
#include <fstream>
#include <stdio.h>
#include <stdlib.h>
#include <string>
#include <ctime>
#include <vector>

using namespace std;

GtkWidget *labText;

static void on_open_response (GtkDialog *dialog, int response)
    if (response == GTK_RESPONSE_ACCEPT)
        GtkFileChooser *chooser = GTK_FILE_CHOOSER (dialog);

        cout << " chooser" << endl;

        string fileName = gtk_file_chooser_get_current_name(chooser);
        cout << "file name: " << fileName << endl;
    gtk_window_destroy (GTK_WINDOW (dialog));

void OpenDialog (GtkWidget* wid, GtkWidget* data)
    cout << "open"<< endl;

    GtkWidget *dialog;
    GtkFileChooserAction action = GTK_FILE_CHOOSER_ACTION_OPEN;

    dialog = gtk_file_chooser_dialog_new ("Open File", GTK_WINDOW(data), action, ("_Cancel"), GTK_RESPONSE_CANCEL,
                                          ("_Open"), GTK_RESPONSE_ACCEPT, NULL);

    gtk_widget_show (dialog);
    g_signal_connect (dialog, "response", G_CALLBACK (on_open_response),  NULL);

static void appActivate (GApplication *app, gpointer user_data)
    GtkWidget *win = gtk_application_window_new (GTK_APPLICATION (app));

    GtkWidget  *vBox = gtk_box_new (GTK_ORIENTATION_VERTICAL,10);
    gtk_window_set_child (GTK_WINDOW (win), vBox);

    labText = gtk_label_new("startovaci text");
    gtk_box_append (GTK_BOX (vBox), labText);

    GtkWidget *btnNeco = gtk_button_new_with_label("choose file");
    gtk_box_append  (GTK_BOX (vBox), btnNeco);
    g_signal_connect_swapped(GTK_BUTTON(btnNeco), "clicked", G_CALLBACK(OpenDialog), win);

    GtkWidget *btnBack = gtk_button_new_with_label("Close");
    gtk_box_append  (GTK_BOX (vBox), btnBack);
    g_signal_connect_swapped(GTK_BUTTON(btnBack), "clicked", G_CALLBACK(gtk_window_destroy), win);

    gtk_widget_show (win);

int main(int argc, char **argv)
    GtkApplication *app;
    app = gtk_application_new ("", G_APPLICATION_FLAGS_NONE);
    g_signal_connect (app, "activate", G_CALLBACK (appActivate), NULL);
    return g_application_run (G_APPLICATION (app), NULL, NULL);
    g_object_unref (app);

    return 0;

thank for any advice

what about something like

gchar *filename = gtk_file_chooser_get_filename (GTK_FILE_CHOOSER (dialog));

in the on_open_response function? I haven’t tested, but something like that should work.

I am looking at

where they use that, but they are reacting to the GTK_RESPONSE_OK signal, it might be different when reacting to GTK_RESPONSE_ACCEPT, I don’t know for sure.

I think I have explained that in full detail here for GTK4:

Are you using GTK4, or still old GTK3? I think in GTK3 dialogs were a bit different, but I can not remember details.

Thank you for reply. But your solution si probably from GTK3, but I need GTK4.

1 Like

The “Getting Started” tutorial on has a section on how to open a file and read its contents.

Again thanks a lot. Yor answer helps me to find solution:

static void on_open_response (GtkDialog *dialog, int response)
    if (response == GTK_RESPONSE_ACCEPT)
         g_autoptr (GFile) file = gtk_file_chooser_get_file (GTK_FILE_CHOOSER (dialog));

         string fileName = g_file_get_path(file);
        cout << "file name: " << fileName << endl;
    gtk_window_destroy (GTK_WINDOW (dialog));

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.