📄 treewidget.html
字号:
gtk_tree_store_append (store, &iter2, &iter1);gtk_tree_store_set (store, &iter2, TITLE_COLUMN, "Volume 2: Seminumerical Algorithms", -1);gtk_tree_store_append (store, &iter2, &iter1);gtk_tree_store_set (store, &iter2, TITLE_COLUMN, "Volume 3: Sorting and Searching", -1);</pre></div></div><div class="refsect1" lang="en"><a name="id3670963"></a><h2>Creating the view component</h2><p> While there are several different models to choose from, there is only one view widget to deal with. It works with either the list or the tree store. Setting up a <a class="link" href="GtkTreeView.html" title="GtkTreeView"><span class="type">GtkTreeView</span></a> is not a difficult matter. It needs a <a class="link" href="GtkTreeModel.html" title="GtkTreeModel"><span class="type">GtkTreeModel</span></a> to know where to retrieve its data from. </p><div class="informalexample"><pre class="programlisting">GtkWidget *tree;tree = gtk_tree_view_new_with_model (GTK_TREE_MODEL (store));</pre></div><div class="refsect2" lang="en"><a name="id3671019"></a><h3>Columns and cell renderers</h3><p> Once the <a class="link" href="GtkTreeView.html" title="GtkTreeView"><span class="type">GtkTreeView</span></a> widget has a model, it will need to know how to display the model. It does this with columns and cell renderers. </p><p> Cell renderers are used to draw the data in the tree model in a way. There are a number of cell renderers that come with GTK+ 2.x, including the <a class="link" href="GtkCellRendererText.html" title="GtkCellRendererText"><span class="type">GtkCellRendererText</span></a>, <a class="link" href="GtkCellRendererPixbuf.html" title="GtkCellRendererPixbuf"><span class="type">GtkCellRendererPixbuf</span></a> and the <a class="link" href="GtkCellRendererToggle.html" title="GtkCellRendererToggle"><span class="type">GtkCellRendererToggle</span></a>. It is relatively easy to write a custom renderer. </p><p> A <a class="link" href="GtkTreeViewColumn.html" title="GtkTreeViewColumn"><span class="type">GtkTreeViewColumn</span></a> is the object that GtkTreeView uses to organize the vertical columns in the tree view. It needs to know the name of the column to label for the user, what type of cell renderer to use, and which piece of data to retrieve from the model for a given row. </p><div class="informalexample"><pre class="programlisting">GtkCellRenderer *renderer;GtkTreeViewColumn *column;renderer = gtk_cell_renderer_text_new ();column = gtk_tree_view_column_new_with_attributes ("Author", renderer, "text", AUTHOR_COLUMN, NULL);gtk_tree_view_append_column (GTK_TREE_VIEW (tree), column);</pre></div><p> At this point, all the steps in creating a displayable tree have been covered. The model is created, data is stored in it, a tree view is created and columns are added to it. </p></div><hr><div class="refsect2" lang="en"><a name="id3671112"></a><h3>Selection handling</h3><p> Most applications will need to not only deal with displaying data, but also receiving input events from users. To do this, simply get a reference to a selection object and connect to the <a class="link" href="GtkTreeSelection.html#GtkTreeSelection-changed"><span class="type">"changed"</span></a> signal. </p><div class="informalexample"><pre class="programlisting">/* Prototype for selection handler callback */static void tree_selection_changed_cb (GtkTreeSelection *selection, gpointer data);/* Setup the selection handler */GtkTreeSelection *select;select = gtk_tree_view_get_selection (GTK_TREE_VIEW (tree));gtk_tree_selection_set_mode (select, GTK_SELECTION_SINGLE);g_signal_connect (G_OBJECT (select), "changed", G_CALLBACK (tree_selection_changed_cb), NULL);</pre></div><p> Then to retrieve data for the row selected: </p><div class="informalexample"><pre class="programlisting">static voidtree_selection_changed_cb (GtkTreeSelection *selection, gpointer data){ GtkTreeIter iter; GtkTreeModel *model; gchar *author; if (gtk_tree_selection_get_selected (selection, &model, &iter)) { gtk_tree_model_get (model, &iter, AUTHOR_COLUMN, &author, -1); g_print ("You selected a book by %s\n", author); g_free (author); }}</pre></div></div></div><div class="refsect1" lang="en"><a name="id3671163"></a><h2>Simple Example</h2><p> Here is a simple example of using a <a class="link" href="GtkTreeView.html" title="GtkTreeView"><span class="type">GtkTreeView</span></a> widget in context of the other widgets. It simply creates a simple model and view, and puts them together. Note that the model is never populated with data — that is left as an exercise for the reader. More information can be found on this in the <a class="link" href="GtkTreeModel.html" title="GtkTreeModel"><span class="type">GtkTreeModel</span></a> section. </p><div class="informalexample"><pre class="programlisting">enum{ TITLE_COLUMN, AUTHOR_COLUMN, CHECKED_COLUMN, N_COLUMNS};voidsetup_tree (void){ GtkTreeStore *store; GtkWidget *tree; GtkTreeViewColumn *column; GtkCellRenderer *renderer; /* Create a model. We are using the store model for now, though we * could use any other GtkTreeModel */ store = gtk_tree_store_new (N_COLUMNS, G_TYPE_STRING, G_TYPE_STRING, G_TYPE_BOOLEAN); /* custom function to fill the model with data */ populate_tree_model (store); /* Create a view */ tree = gtk_tree_view_new_with_model (GTK_TREE_MODEL (store)); /* The view now holds a reference. We can get rid of our own * reference */ g_object_unref (G_OBJECT (store)); /* Create a cell render and arbitrarily make it red for demonstration * purposes */ renderer = gtk_cell_renderer_text_new (); g_object_set (G_OBJECT (renderer), "foreground", "red", NULL); /* Create a column, associating the "text" attribute of the * cell_renderer to the first column of the model */ column = gtk_tree_view_column_new_with_attributes ("Author", renderer, "text", AUTHOR_COLUMN, NULL); /* Add the column to the view. */ gtk_tree_view_append_column (GTK_TREE_VIEW (tree), column); /* Second column.. title of the book. */ renderer = gtk_cell_renderer_text_new (); column = gtk_tree_view_column_new_with_attributes ("Title", renderer, "text", TITLE_COLUMN, NULL); gtk_tree_view_append_column (GTK_TREE_VIEW (tree), column); /* Last column.. whether a book is checked out. */ renderer = gtk_cell_renderer_toggle_new (); column = gtk_tree_view_column_new_with_attributes ("Checked out", renderer, "active", CHECKED_COLUMN, NULL); gtk_tree_view_append_column (GTK_TREE_VIEW (tree), column); /* Now we can manipulate the view just like any other GTK widget */ ...} </pre></div><p> </p></div></div></body></html>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -