Skip to content

GObject.ObjectClass

record (struct)

The class structure for the GObject type.

// Example of implementing a singleton using a constructor.
static MySingleton *the_singleton = NULL;

static GObject*
my_singleton_constructor (GType                  type,
                          guint                  n_construct_params,
                          GObjectConstructParam *construct_params)
{
  GObject *object;

  if (!the_singleton)
    {
      object = G_OBJECT_CLASS (parent_class)->constructor (type,
                                                           n_construct_params,
                                                           construct_params);
      the_singleton = MY_SINGLETON (object);
    }
  else
    object = g_object_ref (G_OBJECT (the_singleton));

  return object;
}

Methods

find_property

def find_property(self, property_name: str) -> ParamSpec

Looks up the ParamSpec for a property of a class.

Parameters:

  • property_name — the name of the property to look up

install_properties

def install_properties(self, pspecs: list[ParamSpec]) -> None

Installs new properties from an array of GParamSpecs.

All properties should be installed during the class initializer. It is possible to install properties after that, but doing so is not recommend, and specifically, is not guaranteed to be thread-safe vs. use of properties on the same type on other threads.

The property id of each property is the index of each ParamSpec in the pspecs array.

The property id of 0 is treated specially by Object and it should not be used to store a ParamSpec.

This function should be used if you plan to use a static array of GParamSpecs and Object.notify_by_pspec. For instance, this class initialization:

typedef enum {
  PROP_FOO = 1,
  PROP_BAR,
  N_PROPERTIES
} MyObjectProperty;

static GParamSpec *obj_properties[N_PROPERTIES] = { NULL, };

static void
my_object_class_init (MyObjectClass *klass)
{
  GObjectClass *gobject_class = G_OBJECT_CLASS (klass);

  obj_properties[PROP_FOO] =
    g_param_spec_int ("foo", NULL, NULL,
                      -1, G_MAXINT,
                      0,
                      G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS);

  obj_properties[PROP_BAR] =
    g_param_spec_string ("bar", NULL, NULL,
                         NULL,
                         G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS);

  gobject_class->set_property = my_object_set_property;
  gobject_class->get_property = my_object_get_property;
  g_object_class_install_properties (gobject_class,
                                     G_N_ELEMENTS (obj_properties),
                                     obj_properties);
}

allows calling Object.notify_by_pspec to notify of property changes:

void
my_object_set_foo (MyObject *self, gint foo)
{
  if (self->foo != foo)
    {
      self->foo = foo;
      g_object_notify_by_pspec (G_OBJECT (self), obj_properties[PROP_FOO]);
    }
 }

Parameters:

  • pspecs — the GParamSpecs array defining the new properties

install_property

def install_property(self, property_id: int, pspec: ParamSpec) -> None

Installs a new property.

All properties should be installed during the class initializer. It is possible to install properties after that, but doing so is not recommend, and specifically, is not guaranteed to be thread-safe vs. use of properties on the same type on other threads.

Note that it is possible to redefine a property in a derived class, by installing a property with the same name. This can be useful at times, e.g. to change the range of allowed values or the default value.

Parameters:

  • property_id — the id for the new property
  • pspec — the ParamSpec for the new property

list_properties

def list_properties(self) -> list[ParamSpec]

Get an array of ParamSpec* for all properties of a class.

override_property

def override_property(self, property_id: int, name: str) -> None

Registers property_id as referring to a property with the name name in a parent class or in an interface implemented by oclass. This allows this class to "override" a property implementation in a parent class or to provide the implementation of a property from an interface.

Internally, overriding is implemented by creating a property of type ParamSpecOverride; generally operations that query the properties of the object class, such as ObjectClass.find_property or ObjectClass.list_properties will return the overridden property. However, in one case, the construct_properties argument of the constructor virtual function, the ParamSpecOverride is passed instead, so that the param_id field of the ParamSpec will be correct. For virtually all uses, this makes no difference. If you need to get the overridden property, you can call ParamSpec.get_redirect_target.

Parameters:

  • property_id — the new property ID
  • name — the name of a property registered in a parent class or in an interface of this class.

Properties

g_type_class

g_type_class: TypeClass  # read/write

construct_properties

construct_properties: list[int]  # read/write

set_property

set_property: Any  # read/write

get_property

get_property: Any  # read/write

dispose

dispose: Any  # read/write

finalize

finalize: Any  # read/write

dispatch_properties_changed

dispatch_properties_changed: Any  # read/write

notify

notify: Any  # read/write

constructed

constructed: Any  # read/write

flags

flags: int  # read/write

n_construct_properties

n_construct_properties: int  # read/write

pspecs

pspecs: int  # read/write

n_pspecs

n_pspecs: int  # read/write

pdummy

pdummy: list[int]  # read/write