FileChooserDialog crash on ESC

Hi,
I’m trying to prompt the user to select a .txt file to use in my Gtk 3 (C) app.
If I simply choose the correct file a press OK or CANCEL everything works, but if i select the txt file and press ESC (instead of clicking OK or cancell button ) the app crash on segmentation fault.
This is my func


static gchar *
SelectFile()
{
  gchar *resPath = NULL;
  GtkWidget *dialog;
  GtkFileChooserAction action = GTK_FILE_CHOOSER_ACTION_OPEN;
  const gchar *ui_file = "/hmi/ui_dir/filechooser.ui";

  GtkBuilder *builder = gtk_builder_new_from_resource(ui_file);

  dialog = GTK_WIDGET(gtk_builder_get_object(builder, "filechooserdialog1"));

  // Imposta le opzioni del dialogo
  gtk_file_chooser_set_action(GTK_FILE_CHOOSER(dialog), action);
  gtk_window_set_title(GTK_WINDOW(dialog), "Select a txt file to upload");
 // gtk_file_chooser_set_select_multiple(GTK_FILE_CHOOSER(dialog), FALSE);
  gtk_file_chooser_set_current_folder(GTK_FILE_CHOOSER(dialog), g_get_home_dir());

  GtkFileFilter *filter = gtk_file_filter_new();
  gtk_file_filter_set_name(filter,"*.txt");
  gtk_file_filter_add_pattern(filter, "*.txt");
  gtk_file_chooser_add_filter(GTK_FILE_CHOOSER(dialog), filter);

  gint response = gtk_dialog_run(GTK_DIALOG(dialog));
  if (response == GTK_RESPONSE_OK)
  {
    gchar *fileName = gtk_file_chooser_get_filename(GTK_FILE_CHOOSER(dialog));
    resPath = g_strdup(fileName);
    g_free(fileName);
  }

  g_object_unref(filter);
  g_object_unref(builder);
  gtk_window_close(GTK_WINDOW(dialog));

  return resPath;
}

and my xml file is like this:

<?xml version="1.0" encoding="UTF-8"?>
<!-- Generated with glade 3.38.2 -->
<interface>
  <requires lib="gtk+" version="3.24"/>
  <object class="GtkFileChooserDialog" id="filechooserdialog1">
    <property name="name">filechooserdialog1</property>
    <property name="can-focus">False</property>
    <property name="tooltip-text" translatable="yes">Select a proper txt file filed with hex value separated by space. Ex: 01 02 03 FF AB</property>
    <property name="type-hint">dialog</property>
    <property name="create-folders">False</property>
    <signal name="response" handler="GTK_RESPONSE_ACCEPT" swapped="no"/>
    <child internal-child="vbox">
      <object class="GtkBox" id="GTK_BOX">
        <property name="can-focus">False</property>
        <property name="orientation">vertical</property>
        <property name="spacing">2</property>
        <child internal-child="action_area">
          <object class="GtkButtonBox" id="GTK_BTNS">
            <property name="can-focus">False</property>
            <property name="margin-end">10</property>
            <property name="layout-style">end</property>
            <child>
              <object class="GtkButton" id="btn_cancel">
                <property name="label" translatable="yes">CANCEL</property>
                <property name="visible">True</property>
                <property name="can-focus">True</property>
                <property name="receives-default">True</property>
                <signal name="clicked" handler="onBtnCancel" swapped="no"/>
              </object>
              <packing>
                <property name="expand">True</property>
                <property name="fill">True</property>
                <property name="position">0</property>
              </packing>
            </child>
            <child>
              <object class="GtkSeparator">
                <property name="width-request">100</property>
                <property name="visible">True</property>
                <property name="can-focus">False</property>
              </object>
              <packing>
                <property name="expand">True</property>
                <property name="fill">True</property>
                <property name="position">4</property>
              </packing>
            </child>
            <child>
              <object class="GtkSeparator">
                <property name="width-request">100</property>
                <property name="visible">True</property>
                <property name="can-focus">False</property>
              </object>
              <packing>
                <property name="expand">True</property>
                <property name="fill">True</property>
                <property name="position">5</property>
              </packing>
            </child>
            <child>
              <object class="GtkSeparator">
                <property name="width-request">100</property>
                <property name="visible">True</property>
                <property name="can-focus">False</property>
              </object>
              <packing>
                <property name="expand">True</property>
                <property name="fill">True</property>
                <property name="position">6</property>
              </packing>
            </child>
            <child>
              <object class="GtkButton" id="btn_ok">
                <property name="label" translatable="yes">OK</property>
                <property name="name">btn_ok</property>
                <property name="visible">True</property>
                <property name="can-focus">True</property>
                <property name="receives-default">True</property>
                <signal name="clicked" handler="onBtn_ok" swapped="no"/>
              </object>
              <packing>
                <property name="expand">True</property>
                <property name="fill">True</property>
                <property name="position">7</property>
              </packing>
            </child>
          </object>
          <packing>
            <property name="expand">False</property>
            <property name="fill">False</property>
            <property name="position">0</property>
          </packing>
        </child>
      </object>
    </child>
    <action-widgets>
      <action-widget response="-7">btn_cancel</action-widget>
      <action-widget response="-5">btn_ok</action-widget>
    </action-widgets>
  </object>
</interface>

What i’m doing wrong? Any help is appreciated
Regards
Dario

gtk_file_chooser_add_filter() takes ownership of the filter, so when you unref it later, you’re removing a reference that isn’t yours.

Side note, I’m not sure that using GtkBuilder for the dialog is doing anything for you. You should consider using a GtkFileChooserNative created in code.

1 Like

Thank you very much for the hint, the issue seems resolved!!!
I also agree with your suggestion on the GtkFileChooserNative, thank you, the only thing that i cannot do is modify the font size only on the filechooserDialog by css file, i cannot understand what keyword to use, my actual css file is:

/* For text Input*/
entry
{
    font-size:30px;
}

/* Increase scrollbar for touch */
scrollbar slider{

    /* Size of the slider */
    min-width: 60px;
    min-height: 60px;
    border-radius: 5px;
 background-color: rgba(49, 54, 74, 0.756);
    /* Padding around the slider */
    border: 5px solid transparent;

}


 scrollbar slider:hover {
      background-color: rgba(0,0,0,0.5);
}

/* Increase label button Font */
button label
{
font-size:30px;
}

/* Increase label*/
label
{
font-size:60px;
}

/* Increase Text View Font */
.view
{
	font-size:20px;
}

I have tried: .filechooser GtkFileChooserDialog but nothing seems to work, only

 filechooser
{

	font-size:20px;
}

But it changes only the font for extension, the rest of the font seems to follow the “label” generic instruction

GtkFileChooserNative is an abstraction. The dialog may not be an in-process GtkFileChooserDialog; instead it could be a dialog created by the FileChooser portal, or a native dialog on another platform.

If you absolutely need to poke at it like that, then you’ll need to keep using GtkFileChooserDialog.

Depending on your motivation for increasing the font size, it might make more sense to change the system settings?

1 Like

mmm it’s a good point, i started by use the builder because i wanted to increase the size of the scrollbar because my device is a screen with no physical input like mouse or keyboard and the scroll was too tiny for the fingers and the text a little bit small. But then i found out how to set the scrollbar size with css in all the app and i think that i can do the same thing for the system font and reserve for my buttons a bigger font in the css file. Thank you again!

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