gpt4 book ai didi

c - 我无法使用 g_object_get 或 g_object_get_property 在 GtkImage 上获取 "file"属性

转载 作者:行者123 更新时间:2023-12-01 02:00:28 27 4
gpt4 key购买 nike

我在 GtkEventBox 中有一个 GtkImage,在 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>

我想做的是在单击 EventBox 时更改图像路径。

我在创建硬编码的事件框之前就这样做了,并且工作得很好。但是现在我无法获得"file"属性。我尝试了两种方法,使用 g_object_getg_object_get_property,但我总是得到一个 NULL 对象作为响应,而不是文件名。

这是我的代码

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;
}

我在互联网上进行了深入搜索,但一无所获。我试图找出另一种解决方案来实现我想要的,但我不知道该怎么做。我将不胜感激任何帮助或建议。单击事件框时如何更改图像文件?非常感谢!

最佳答案

重点是您或更确切地说是 GtkBuilder 没有初始化 GtkImage 对象的 file 属性。所以你得到了 file 的预期默认值,即 NULL

相反,glade 似乎默认将 pixbuf 属性设置为文件路径。这是完全有效的,并记录在引用的 GtkBuilder UI 定义 部分:

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

我换了行

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

你的 ui 定义文件到

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

它在我的系统上按最初预期的方式工作。

关于c - 我无法使用 g_object_get 或 g_object_get_property 在 GtkImage 上获取 "file"属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17914131/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com