Search code examples
cgtkglade

I can't get "file" property on GtkImage using g_object_get, nor g_object_get_property


I've a GtkImage inside a GtkEventBox, defined on glade

 <object class="GtkEventBox" id="eb_paciente">
    <signal name="button-press-event" handler="print_filepath" object="bt_icn_paciente" swapped="no"/>
    <child>
      <object class="GtkImage" id="bt_icn_paciente">
       <property name="pixbuf">img/patient_icons/icon_patient.png</property>
      </object>
    </child>
  </object>

What I'm trying to do is to change the image path when the EventBox is clicked.

I did this before creating the eventbox hardcoded and was working like a charm. But now I can't get the "file" property. I've tried both ways, using g_object_get and g_object_get_property, but I allways get a NULL object as response, never the filename.

Here is my code

gboolean print_filepath(GtkWidget *widget, GdkEvent  *event, gpointer *data){
  GValue value = G_VALUE_INIT;

  g_value_init (&value, G_TYPE_STRING);
  g_object_get_property(G_OBJECT(gtk_bin_get_child(GTK_EVENT_BOX(widget))), "file", &value);
  g_print("\n print_filepath = %s\n",  g_value_get_string(&value));

  gchar *filepath;
  g_object_get(G_OBJECT(data), "file", &filepath, NULL);
  g_print("\n print_filepath = %s\n", filepath);
  g_free(filepath);

  return FALSE;
}

I've done a deep search on internet and found nothing about it. I'm trying to figure out another solution to achieve what I want to but I have no idea what to do. I would appreciate any help or suggetions. What can I do to change the image file when click on a event box? Thanks very much!


Solution

  • The whole point is that you or rather GtkBuilder don't initialize the file property of your GtkImage object. So you get the expected default value of file namely NULL.

    Instead glade seems to set the pixbuf property to the file path by default. That is perfectly valid and documented in the GtkBuilder UI Definitions section of the reference:

    Pixbufs can be specified as a filename of an image file to load

    I changed the line

    <property name="pixbuf">img/patient_icons/icon_patient.png</property>
    

    of your ui definitions file to

    <property name="file">img/patient_icons/icon_patient.png</property>
    

    and it works as initially expected on my system.