Adventist Adventurer Awards/Buttons - Wikibooks, open books for an open world

GTK+ By Example/Tree View/Tree View

Making a Tree View[edit]

With the intention to show knowledge in a tree view widget, we have to create one first, and we have to instruct it the place to get the info to show from.

A brand new tree view is created with:

  GtkWidget *view;

  view = gtk_tree_view_new();

Connecting Tree View and Mannequin[edit]

Earlier than we proceed to the following part the place we show knowledge on the display screen, we’d like join our knowledge retailer to the tree view, so it is aware of the place to get the info to show from. That is achieved with gtk_tree_view_set_model, which can by itself do little or no. Nonetheless, it’s a prerequisite for what we do within the following sections. gtk_tree_view_new_with_model is a comfort operate for the earlier two.

gtk_tree_view_get_model will return the mannequin that’s presently hooked up to a given tree view, which is especially helpful in callbacks the place you solely get handed the tree view widget (in any case, we don’t wish to go down the street of world variables, which can inevitably result in the Darkish Facet, can we?).

Reference counting[edit]

Tree fashions like GtkListStore and GtkTreeStore are GObjects and have a reference rely of 1 after creation. The tree view will add its personal reference to the mannequin whenever you add the mannequin with gtk_tree_view_set_model, and can unref it once more whenever you substitute the mannequin with one other mannequin, unset the mannequin by passing NULL as a mannequin, or when the tree view is destroyed. [1]

Which means you should handle “your” reference your self, in any other case the mannequin won’t be destroyed correctly whenever you disconnect it from the tree view, and its reminiscence won’t be freed (which doesn’t matter a lot if the identical mannequin is linked to the tree view from software begin to finish). When you plan to make use of the identical mannequin for a tree view for the entire length of the applying, you may do away with “your” reference proper after you could have linked the mannequin to the view – then the mannequin shall be destroyed robotically when the tree view is destroyed (which shall be robotically destroyed when the window it’s in is destroyed):

  GtkListStore *liststore;
  GtkWidget    *view;

  view = gtk_tree_view_new();

  liststore = gtk_list_store_new(1, G_TYPE_STRING);

  gtk_tree_view_set_model(GTK_TREE_VIEW(view), GTK_TREE_MODEL(liststore));


  /* Now the mannequin shall be destroyed when the tree view is destroyed */


‘Reference counting’ implies that an object has a counter that may be elevated or decreased (ref-ed and unref-ed). If the counter is unref-ed to 0, the item is robotically destroyed. That is helpful, as a result of different objects or software programmers solely have to consider whether or not they themselves are nonetheless utilizing that object or not, with out understanding something about others additionally utilizing it. The thing is just robotically destroyed when nobody is utilizing it any extra.

Tree View Look and Really feel[edit]

There are a few methods to affect the appear and feel of the tree view. You possibly can conceal or present column headers with gtk_tree_view_set_headers_visible, and set them clickable or not with gtk_tree_view_set_headers_clickable (which shall be performed robotically for you for those who allow sorting).

gtk_tree_view_set_rules_hint will allow or disable guidelines within the tree view. [1] Because the operate title implies, this setting is simply a touch; in the long run it depends upon the energetic Gtk+ theme engine if the tree view exhibits dominated traces or not. Customers appear to have robust emotions about guidelines in tree views, so it’s most likely a good suggestion to offer an possibility someplace to disable rule hinting for those who set it on tree views (however then, folks additionally appear to have robust emotions about choices abundance and ‘wise’ default choices, so no matter you do will most likely upset somebody sooner or later).

The expander column might be set with gtk_tree_view_set_expander_column. That is the column the place youngster components are indented with respect to their mother and father, and the place rows with kids have an ‘expander’ arrow with which a node’s kids might be collapsed (hidden) or expanded (proven). By default, that is the primary column.

‘Guidelines’ implies that each second line of the tree view has a shaded background, which makes it simpler to see which cell belongs to which row in tree views which have lots of columns.

Author: admin

Leave a Reply

Your email address will not be published. Required fields are marked *