2

I try to inherit a gobject and, among other things, would like to change the value of a G_PARAM_CONSTRUCT_ONLY property so the next child class doesn't have to care.

Here's an example to depict this: GtkComboBox has a construct only property called "has-entry" with default value FALSE. In class A I want to change this value to TRUE, so that class B doesn't need to care.

            GtkComboBoxClass <-- AClass <-- BClass
"has-entry"      FALSE            TRUE

The first naive approach was to use g_object_set() in A's instance_init function, but to no avail.

The next idea was to obtain the GParamSpec with g_object_class_find_property() and change the default value with g_param_value_set_default() in A's class_init function. But I suppose this to change the default for all GtkComboBoxClass derived objects.

The best idea I could come up with: If g_object_class_override_property() creates a new GParamSpec I could find this and set its default value in A's class_init function. But documentation doesn't loose a word about this.

So my question: Is this a working, and intended, way of accomplishing this, or is there a better solution?

Tried so far:

  • g_object_set() in instance init():
    • no warning on start
    • no effect
  • g_object_set() in GObjectClass->constructor():
    • no warning on start
    • no effect
    • warning on exit: invalid cast from GtkCellCiew to GtkEntry
  • g_object_set() in GObjectClass->constructed():
    • warning on start: can't be set after construction

Thanks Stefan

2 Answers 2

3

if you want to set a property in a sub-class, and that property is construct-only, then you should use the constructed virtual function to call g_object_set() instead of the init virtual.

properties marked as construct-only will be applied during construction, using their default value, unless specified on the constructor itself — i.e. with g_object_new(). this means that setting a construct-only property inside init() will not suffice, as the value will be set after init() has been called. the constructed() virtual function, on the other hand, is called after the constructor properties have been applied, so it's possible to override the default value there.

1
  • Tried this with glib-2.28.8. Get: GLib-GObject-WARNING **: g_object_set_valist: construct property "has-entry" for object `TestComboBox' can't be set after construction May 16, 2013 at 3:29
1

Answering this for myself:

A look into gobject source reveals that the properties list given to constructor() contains all G_PARAM_CONSTRUCT and G_PARAM_CONSTRUCT_ONLY properties and their default or given values.

Modifying these values is undocumented (or at least I couldn't find it), but it works. Construction time property values have to be modified in this list before chaining up to parents constructor, non construct properties have to be set afterwards. Example code looks like:

static GObject *constructor(GType gtype, guint n_properties, GObjectConstructParam *properties) {
    GObject *object;
    guint i;
    gchar const *name;
    GObjectConstructParam *property;

    for (i = 0, property = properties; i < n_properties; ++i, ++property) {
        name = g_param_spec_get_name(property->pspec);
        if (!strcmp(name, "has-entry")) // is G_PARAM_CONSTRUCT_ONLY
            g_value_set_boolean(property->value, TRUE);
    }

    object = G_OBJECT_CLASS(parent_class)->constructor(gtype, n_properties, properties);

    g_object_set(object, "entry-text-column", TEXT_COLUMN, NULL);

    return object;
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.