krokoziabla
krokoziabla

Reputation: 775

GObject: how to reset property to the default value?

I have a GObject which has a property of type GObject. I know that I can set this property like this:

g_object_set (G_OBJECT (my_object), "my-property", my_value_for_property, NULL);

But how do I reset the property to its default value? Probably, this line seems intuitive:

g_object_set_property (G_OBJECT (my_object), "my-property", NULL);

But what if I the default value ob "my-property" is non-null pointer to object? And anyway this line doesn't work. It seems I cannot just pass NULL to g_object_set_property()

Upvotes: 1

Views: 1075

Answers (1)

Philip Withnall
Philip Withnall

Reputation: 5703

You need to get the GParamSpec for the property (essentially, the definition of the property on the class) using g_object_class_find_property(), then get its default value using g_param_spec_get_default_value().

Something like the following should work:

GParamSpec *pspec = g_object_class_find_property (G_OBJECT_GET_CLASS (my_object), "my-property");
const GValue *default_value = g_param_spec_get_default_value (pspec);
g_object_set_property (my_object, "my-property", default_value);

Upvotes: 5

Related Questions