How do you set an enum property on a GLib object?

自闭症网瘾萝莉.ら 提交于 2020-02-16 05:49:28

问题


I am trying to set the "ellipsize" enum property on a GtkCellRendererText object.

I am trying to use g_object_set_property as follows:

GtkCellRenderer *renderer = gtk_cell_renderer_text_new ();

GValue val = G_VALUE_INIT;
g_value_init (&val, G_TYPE_ENUM);
g_value_set_enum (&val, PANGO_ELLIPSIZE_END);
g_object_set_property (G_OBJECT(renderer), "ellipsize", &val);

However, I get an error message at run time:

(infog:27114): GLib-GObject-WARNING **: 12:24:29.848: ../../../../gobject/gvalue.c:188: cannot initialize GValue with type 'GEnum', this type is abstract with regards to GValue use, use a more specific (derived) type

How do I get the type ID for enum PangoEllipsizeMode that derives from G_TYPE_ENUM?


回答1:


You need to initialise the GValue container with the type of the enumeration that the property expects. G_TYPE_ENUM is the generic, abstract enumeration type.

The "ellipsize" property of GtkCellRendererText expects a PangoEllipsizeMode enumeration value, which has a GType of PANGO_TYPE_ELLIPSIZE_MODE.

GtkCellRenderer *renderer = gtk_cell_renderer_text_new ();

GValue val = G_VALUE_INIT;

g_value_init (&val, PANGO_TYPE_ELLIPSIZE_MODE);
g_value_set_enum (&val, PANGO_ELLIPSIZE_END);

g_object_set_property (G_OBJECT(renderer), "ellipsize", &val);

// Always unset your value to release any memory that may be associated with it
g_value_unset (&val);



回答2:


Using g_object_set instead works:

g_object_set (G_OBJECT(renderer), "ellipsize", PANGO_ELLIPSIZE_END, NULL);


来源:https://stackoverflow.com/questions/58540419/how-do-you-set-an-enum-property-on-a-glib-object

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!