您的示例具有误导性:您可能还有其他代码未显示的问题。
以下测试用例使用您相同的循环并且不返回NULL
。ui 文件没有name
对对象进行任何设置。试试看。
#include <gtk/gtk.h>
int main()
{
GtkBuilder *builder;
GSList *lp, *lst;
gtk_init(NULL, NULL);
builder = gtk_builder_new_from_file("test.ui");
lst = gtk_builder_get_objects(builder);
g_print("%p\n", lst);
for (lp = lst; lp != NULL; lp = lp->next) {
g_print("%p\n", (char *)(lp->data));
}
return 0;
}
这是test.ui
:
<?xml version="1.0" encoding="UTF-8"?>
<interface>
<requires lib="gtk+" version="2.24"/>
<!-- interface-naming-policy project-wide -->
<object class="GtkWindow" id="window1">
<property name="can_focus">False</property>
<child>
<object class="GtkVBox" id="vbox1">
<property name="visible">True</property>
<property name="can_focus">False</property>
<child>
<object class="GtkLabel" id="label1">
<property name="visible">True</property>
<property name="can_focus">False</property>
<property name="label" translatable="yes">label</property>
</object>
<packing>
<property name="expand">True</property>
<property name="fill">True</property>
<property name="position">0</property>
</packing>
</child>
<child>
<object class="GtkButton" id="button1">
<property name="label" translatable="yes">button</property>
<property name="visible">True</property>
<property name="can_focus">True</property>
<property name="receives_default">True</property>
</object>
<packing>
<property name="expand">True</property>
<property name="fill">True</property>
<property name="position">1</property>
</packing>
</child>
</object>
</child>
</object>
</interface>