类方法
GObjectObjectClassinstall_properties
since: 2.26
声明 [src]
void
g_object_class_install_properties (
GObjectClass* oclass,
guint n_pspecs,
GParamSpec** pspecs
)
描述 [src]
安装从 GParamSpecs
数组中来的新属性。
所有属性都应该在类初始化时安装。之后安装属性是可能的,但这样做不建议,并且特别指出,可能不是线程安全的,与其他线程上同一类型的属性使用相比。
每个属性的属性 id 是 pspecs
数组中每个 GParamSpec
的索引。
0 的属性 id 由 GObject
特殊处理,不应用来存储 GParamSpec
。
如果您计划使用静态的 GParamSpecs
数组并同时使用 g_object_notify_by_pspec(),则应使用此函数。例如,此类的初始化
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);
}
允许调用 g_object_notify_by_pspec()
来通知属性变化
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]);
}
}
Available since: 2.26