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

StackOverflow https://stackoverflow.com/questions/17914131

  •  04-06-2022
  •  | 
  •  

Question

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!

Était-ce utile?

La 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.

Autres conseils

from answer of user1146332 changing the line

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

to

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

will allow you to get the file name. Thanks to you.

In python, you get the filename like this:

ImageFileName = widget.get_property('file')

If you plan on designing on glade and modifying your *.glade file. You will have to modify it everytime. Here is some code to "repair" that file. I've done it in Python, if it's of use for anyone.

from lxml import etree
import xml.etree.ElementTree as ET

class ModifyXML:
    def ChangePropertyPixbuf():


    print("start")


#____________________________________________


        tree = ET.parse("test image.glade")

        root = tree.getroot()


        for ChgValue in root.findall(".//*[@class='GtkImage']"):

            ChgValueb = ChgValue.find(".//*[@name='pixbuf']")

            if ChgValueb is not None:

                ValueOfb = ChgValueb.text

                if ChgValueb.text != "" : ChgValue.remove(ChgValueb)

                ChgValueb = ET.SubElement(ChgValue,'property')

                ChgValueb.set("name", "file")

                ChgValueb.text = ValueOfb

        tree.write("test image.glade")

#_________________________________________

print("done")


ModifyXML.ChangePropertyPixbuf()
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top