cgtk4

How to add FileFilter to FileDialog GTK4


Could someone explain me, how to add FileFilter to FileDialog GTK4?

I think, I have a wrong decision, it's not working.

Ive created GTKFileFilter* object and trying to add it to GTKFileDialog* object:

GtkFileDialog* fileDialog=gtk_file_dialog_new();
GtkFileFilter* filefilter = gtk_file_filter_new();
gtk_file_filter_add_suffix(filefilter,"tsc");
gtk_file_filter_set_name(filefilter,"Simple");
gtk_file_dialog_set_filters(fileDialog,filefilter);

But gtk_file_dialog_set_filters() expect a GListModel* as second parameter, so I've got assertion:

Gtk-CRITICAL **: 09:44:48.986: gtk_file_dialog_set_filters: assertion 'filters == NULL || G_IS_LIST_MODEL (filters)' failed

How can I create a right filters?


Solution

  • I tried it out and here is my suggestion.

    #include<gtk/gtk.h>
    
    
      GtkFileDialog *filedialog = gtk_file_dialog_new();
    
      GtkFileFilter* filefilter = gtk_file_filter_new();
      gtk_file_filter_add_suffix(filefilter,"txt");
      gtk_file_filter_set_name(filefilter,"Text");
    
      GtkFileFilter *filefilter1 = gtk_file_filter_new();
      gtk_file_filter_add_suffix(filefilter1,"c");
      gtk_file_filter_set_name(filefilter1,"c-file");
    
      GListStore* liststore = g_list_store_new (GTK_TYPE_FILE_FILTER);
      g_list_store_append(liststore, filefilter);
      g_list_store_append(liststore, filefilter1);
    
      gtk_file_dialog_set_filters(filedialog,G_LIST_MODEL(liststore));
    
      gtk_file_dialog_open(filedialog,GTK_WINDOW(window),NULL,NULL,NULL);
    

    The implementation of a list model can also be found here

    https://stackoverflow.com/a/77759424/22768315

    https://stackoverflow.com/a/78185115/22768315

    https://stackoverflow.com/a/77619798/22768315

    Regards