Search code examples
cglibgobject

Init GValue for a struct


I am trying to get a GdkRGBA from a GValue, but I am unable to init a GValue with such a type.

I tried the following code:

GValue value;
g_value_init(value, G_TYPE_OBJECT);
gtk_style_context_get_property(style_context, "color", STATE_FLAG_NORMAL, &value);

But I get the following error:

cannot initialize GValue with type 'GdkRGBA', the value has already been initialized as 'GObject'

I tried many of the other G_TYPE_*, but I get a similar error.

How can I initialize a GValue with a type GdkRGBA?


Solution

  • Try removing the call to g_value_init altogether—judging by the error message it seems gtk_style_context_get_property wants to initialize the GValue on its own.

    Also, I note there is a separate method defined on GtkStyleContext specifically for retrieving the foreground colour, so this may be a suitable (or preferable) alternative:

    GdkRGBA color;
    gtk_style_context_get_color(style_context, GTK_STATE_FLAG_NORMAL, &color);