我已经阅读了 Glade 的一些手册,并创建了一个 GUI,其中有一个我想通过代码填充的 TreeView。林间空地文件是:
<?xml version="1.0" encoding="UTF-8"?>
<interface>
<!-- interface-requires gtk+ 3.0 -->
<!-- interface-naming-policy toplevel-contextual -->
<object class="GtkWindow" id="tree_window">
<property name="can_focus">False</property>
<property name="border_width">3</property>
<property name="title" translatable="yes">Tree Viewer</property>
<property name="default_width">400</property>
<property name="default_height">600</property>
<signal name="destroy" handler="gtk_main_quit" swapped="no"/>
<child>
<object class="GtkVBox" id="vbox1">
<property name="visible">True</property>
<property name="can_focus">False</property>
<child>
<object class="GtkTreeView" id="treeview1">
<property name="visible">True</property>
<property name="can_focus">True</property>
<child>
<object class="GtkTreeViewColumn" id="aColumn">
<property name="title" translatable="yes">A</property>
</object>
</child>
<child>
<object class="GtkTreeViewColumn" id="bColumn">
<property name="title" translatable="yes">B</property>
</object>
</child>
</object>
<packing>
<property name="expand">True</property>
<property name="fill">True</property>
<property name="position">0</property>
</packing>
</child>
</object>
</child>
</object>
</interface>
所以这个想法是用我在下面的代码中定义的 GtkTreeStore 填充“treeview1”:
#include <gtk/gtk.h>
enum{
A_COL=0,
B_COL
};
GtkTreeModel * fill_gtk_tree_store_from_xml_file()
{
GtkTreeStore * t_model;
t_model=gtk_tree_store_new(2,G_TYPE_UINT,G_TYPE_UINT);
GtkTreeIter toplevel,childlevel;
gtk_tree_store_append(t_model,&toplevel,NULL);
gtk_tree_store_append(t_model,&toplevel,NULL);
gtk_tree_store_append(t_model,&toplevel,NULL);
gtk_tree_store_set(t_model,&toplevel,A_COL,(guint)12,B_COL,(guint)14,-1);
gtk_tree_store_append(t_model, &childlevel, &toplevel);
gtk_tree_store_set(t_model,&childlevel,0,(guint)20,
1,(guint)22,-1);
gtk_tree_store_append(t_model, &childlevel, &toplevel);
return GTK_TREE_MODEL (t_model);
}
#include <gtk/gtk.h>
#include <stdlib.h>
int launchGUI(GtkBuilder *builder,GError *error=NULL)
{
GtkWidget *ventanaPrincipal;
if( ! gtk_builder_add_from_file( builder,"treeStore.glade", &error ) )
{
g_warning( "%s", error->message );
g_free( error );
return( 1 );
}
ventanaPrincipal = GTK_WIDGET(gtk_builder_get_object(builder, "tree_window"));
GtkTreeModel *model;
GtkWidget *view;
model=fill_gtk_tree_store_from_xml_file();
view=GTK_WIDGET(gtk_builder_get_object(builder, "treeview1"));
gtk_tree_view_set_model (GTK_TREE_VIEW (view), model);
gtk_tree_selection_set_mode(gtk_tree_view_get_selection(GTK_TREE_VIEW(view)),
GTK_SELECTION_NONE);
gtk_signal_connect(GTK_OBJECT(ventanaPrincipal), "destroy",
G_CALLBACK(gtk_main_quit), NULL);
gtk_widget_show_all(ventanaPrincipal);
gtk_main();
return 0;
}
int main(int argc,char **argv)
{
GtkBuilder *gtkBuilder;
GError *error = NULL;
gtk_init(&argc, &argv);
gtkBuilder= gtk_builder_new();
launchGUI(gtkBuilder,error);
return EXIT_SUCCESS;
}
我尝试了不同的方法,使用枚举,不使用枚举......空行显示为空(确定),但不应为空的行也显示为空。当我运行代码时,会发生这种情况: 图像
我不知道我做错了什么,有人可以帮助我吗?