Types 如何在glib中查找已注册枚举类型的全名?

Types 如何在glib中查找已注册枚举类型的全名?,types,enums,glib,Types,Enums,Glib,我有许多通过标准glib注册函数创建的枚举: GType foo_type = g_enum_register_static("Foo", foo_enum_values); 但是,当我尝试恢复注册枚举的名称(“Foo”)时,我得到的是它的基本类: gchar const * type_name = g_type_get_name(foo_type); printf("%s\n",type_name); 打印“GEnum”而不是“Foo”。只有注册的类

我有许多通过标准glib注册函数创建的枚举:

GType foo_type = g_enum_register_static("Foo", foo_enum_values);
但是,当我尝试恢复注册枚举的名称(“Foo”)时,我得到的是它的基本类:

gchar const * type_name = g_type_get_name(foo_type);
printf("%s\n",type_name);

打印“GEnum”而不是“Foo”。只有注册的类型id,我如何才能取回字符串“Foo”?

我无法完全检查您的代码,因为您没有提供最低限度的工作复制程序,但以下代码对我来说很好:

/* gcc -o test test.c $(pkg-config --cflags --libs glib-2.0 gobject-2.0) */
#include <glib.h>
#include <glib-object.h>

static const GEnumValue my_enum_values[] =
{
  { 1, "the first value", "one" },
  { 2, "the second value", "two" },
  { 3, "the third value", "three" },
  { 0, NULL, NULL }
};

int
main (void)
{
  GType type;

  type = g_enum_register_static ("MyEnum", my_enum_values);

  g_assert_cmpstr (g_type_name (type), ==, "MyEnum");

  return 0;
}
/*gcc-o test test.c$(pkg配置--cflags--libs glib-2.0 gobject-2.0)*/
#包括
#包括
静态常量GEnumValue my_enum_值[]=
{
{1,“第一个值”,“一”},
{2,“第二个值”,“两个”},
{3,“第三个值”,“三”},
{0,NULL,NULL}
};
int
主(空)
{
G型;
type=g_enum_register_static(“MyEnum”,my_enum_值);
g_assert_cmpstr(g_type_name(type),==,“MyEnum”);
返回0;
}

您的代码确实给出了正确的结果,而我在大量工作中几乎相同的代码却没有给出正确的结果。我要试着找出原因。至少现在我知道我看到的g_type_name的结果不是预期的结果。在这一点上,我的最佳猜测是我以某种方式存储了父类型,而不是新创建的类型……您的最佳猜测对我来说似乎是一个很好的调查起点。祝你好运