With the introduction of gtkcombobox and gtkliststore, it is not a concern to use gtktreestore. It is only a part of the parent-child relationship.
If you want to configure a zookeeper structure, you must use it with gtktreestore. The main difference is that gtktreestroe has a parent-child relationship, therefore, when adding a child vertex, you must specify the parent vertex. You can use the gtk_tree_store_append () function to add the child vertex. You can use the gtk_tree_store_set () function to set the vertex information () function:
Void gtk_tree_store_append (gtktreestore * tree_store,
Gtktreeiter * ITER,
Gtktreeiter * parent );
Void gtk_tree_store_set (gtktreestore * tree_store,
Gtktreeiter * ITER,
...);
When using gtk_tree_store_append (), if there is no parent vertex, the third vertex is set to null, indicating that this is the top vertex. Therefore, you must have two gtktreeiter, one pointing to the parent vertex location in the current gtktreestore, And the other pointing to the Child vertex location.
The following code changesGtkcombobox and gtkliststore,Make it have a sub-choice. In the gtkcombobox, the content of the sub-choice is displayed as a sub-choice. The content of the sub-choice is determined by the number of arguments, the changes to the program are mainly made in the model:
- Gtk_combo_box_with_tree_demo.c
#include <gtk/gtk.h>enum { PIXBUF_COL, TEXT_COL};GtkTreeModel* createModel() { const gchar *files[] = {"caterpillar.jpg", "momor.jpg", "hamimi.jpg", "bush.jpg"}; gchar *stocks[] = {
GTK_STOCK_DIALOG_WARNING,
GTK_STOCK_STOP,
GTK_STOCK_NEW,
GTK_STOCK_CLEAR,
GTK_STOCK_OPEN
};
gchar *stockNames[] = {
"WARNING",
"STOP",
"NEW",
"GTK_STOCK_CLEAR",
"GTK_STOCK_OPEN"
}; GtkWidget *cellView; GdkPixbuf *pixbuf; GtkTreeIter iter1, iter2; GtkTreeStore *store; gint i, j, s; store = gtk_tree_store_new(2, GDK_TYPE_PIXBUF, G_TYPE_STRING); cellView = gtk_cell_view_new(); for(i = 0; i < 4; i++) { pixbuf = gdk_pixbuf_new_from_file(files[i], NULL); gtk_tree_store_append(store, &iter1, NULL);
gtk_tree_store_set(store, &iter1,
PIXBUF_COL, pixbuf,
TEXT_COL, files[i],
-1);
gdk_pixbuf_unref(pixbuf); for(j = 0; j < 3; j++) {
s = rand() % 5;
pixbuf = gtk_widget_render_icon(cellView, stocks[s],
GTK_ICON_SIZE_BUTTON, NULL);
gtk_tree_store_append(store, &iter2, &iter1);
gtk_tree_store_set(store, &iter2,
PIXBUF_COL, pixbuf,
TEXT_COL, stockNames[s],
-1);
gdk_pixbuf_unref(pixbuf);
} } return GTK_TREE_MODEL(store);}gboolean combo_changed(GtkComboBox *comboBox, GtkLabel *label) { GtkTreeModel *model = gtk_combo_box_get_model(comboBox); GtkTreeIter iter; gchar *active; gtk_combo_box_get_active_iter(comboBox, &iter); gtk_tree_model_get(model, &iter, 1, &active, -1); gtk_label_set_text(label, active);}int main(int argc, char *argv[]) { GtkWidget *window; GtkWidget *comboBox; GtkCellRenderer *renderer; GtkWidget *label; GtkWidget *vbox; gtk_init(&argc, &argv); window = gtk_window_new(GTK_WINDOW_TOPLEVEL); gtk_window_set_title(GTK_WINDOW(window), "GtkComboBox"); gtk_window_set_default_size(GTK_WINDOW(window), 200, 50); comboBox = gtk_combo_box_new_with_model(createModel()); gtk_combo_box_set_active(GTK_COMBO_BOX(comboBox), 0); renderer = gtk_cell_renderer_pixbuf_new(); gtk_cell_layout_pack_start(GTK_CELL_LAYOUT(comboBox), renderer, FALSE); gtk_cell_layout_set_attributes(GTK_CELL_LAYOUT(comboBox), renderer, "pixbuf", PIXBUF_COL, NULL); renderer = gtk_cell_renderer_text_new(); gtk_cell_layout_pack_start(GTK_CELL_LAYOUT(comboBox), renderer, FALSE); gtk_cell_layout_set_attributes(GTK_CELL_LAYOUT(comboBox), renderer, "text", TEXT_COL, NULL); label = gtk_label_new("caterpillar.jpg"); vbox = gtk_vbox_new(TRUE, 5); gtk_box_pack_start(GTK_BOX(vbox), comboBox, TRUE, TRUE, 5); gtk_box_pack_start(GTK_BOX(vbox), label, TRUE, TRUE, 5); gtk_container_add(GTK_CONTAINER(window), vbox); g_signal_connect(GTK_OBJECT(comboBox), "changed", G_CALLBACK(combo_changed), label); g_signal_connect(GTK_OBJECT(window), "destroy", G_CALLBACK(gtk_main_quit), NULL); gtk_widget_show_all(window); gtk_main(); return 0;}
The results of a line are as follows: