用GtkTreeView实现ListBox的示例

GtkTreeView实现ListBox示例

 

转载时请注明出处:http://blog.csdn.net/absurd/

 

前段时间花了不少时间去研究GTK+的架构,但没有怎么关注具体控件的使用。大部控件都有示例代码,相信使用不会很难。今天要测试一下用GtkTreeView实现ListBox,发现没有GtkTreeView的示例代码,GTK+tutorial里也没有讲解。只好自己折腾,结果花了两个小时才搞定,写段代码做个备忘吧。

 

GtkWidget*

create_window1 (void)

{

  GtkWidget *window1;

  GtkWidget *scrolledwindow1;

  GtkWidget *treeview1;

  GtkTreeViewColumn* column;

  GtkTreeIter iter;

  GtkListStore* store;

  GtkCellRenderer* cell;

 

  char text[32] = {0};

  int i = 0 ;

  int n = 100;

 

  window1 = gtk_window_new (GTK_WINDOW_TOPLEVEL);

  gtk_window_set_title (GTK_WINDOW (window1), _("window1"));

 

  scrolledwindow1 = gtk_scrolled_window_new (NULL, NULL);

  gtk_widget_show (scrolledwindow1);

  gtk_container_add (GTK_CONTAINER (window1), scrolledwindow1);

  gtk_scrolled_window_set_shadow_type (GTK_SCROLLED_WINDOW (scrolledwindow1), GTK_SHADOW_IN);

 

  treeview1 = gtk_tree_view_new ();

  column = gtk_tree_view_column_new();

  gtk_tree_view_append_column(GTK_TREE_VIEW(treeview1), column);

 

  cell = gtk_cell_renderer_text_new();

  gtk_tree_view_column_pack_start(column, cell, TRUE);

  gtk_tree_view_column_set_attributes(column, cell, "text", 0, NULL);

 

  column = gtk_tree_view_column_new();

  gtk_tree_view_append_column(GTK_TREE_VIEW(treeview1), column);

 

  store = gtk_list_store_new(1, G_TYPE_STRING);

  gtk_tree_view_set_model(treeview1, GTK_TREE_MODEL(store));

 

  for(i = 0; i < n; i ++)

  {

    sprintf(text, "text%d", i);

    gtk_list_store_append(store, &iter);

    gtk_list_store_set(store, &iter, 0, text, -1);

  }

 

  gtk_widget_show (treeview1);

  gtk_container_add (GTK_CONTAINER (scrolledwindow1), treeview1);

 

  /* Store pointers to all widgets, for use by lookup_widget(). */

  GLADE_HOOKUP_OBJECT_NO_REF (window1, window1, "window1");

  GLADE_HOOKUP_OBJECT (window1, scrolledwindow1, "scrolledwindow1");

  GLADE_HOOKUP_OBJECT (window1, treeview1, "treeview1");

 

  return window1;

} 

 

2006-11-12补充:

得到选中项的内容:

void

on_button1_clicked                     (gpointer user_data, GtkButton       *button)

{

    GValue value                = {0};

    GtkTreeIter iter            = {0};

    GtkTreeStore* store         = NULL;

    GtkTreeSelection* selection = NULL;

    GtkTreeView* treeview       = NULL;

 

    treeview = (GtkTreeView*)lookup_widget(GTK_WIDGET(user_data), "treeview1");

    selection = gtk_tree_view_get_selection(treeview);

    gtk_tree_selection_get_selected(selection, (GtkTreeModel**)&store, &iter);

    gtk_tree_model_get_value(GTK_TREE_MODEL(store), &iter, 0, &value);

 

    g_debug("%s/n", g_value_get_string(&value));

 

    return ;

}

 

~~end~~

 

你可能感兴趣的:(object,tree,null,attributes,gtk,Pointers)