Analysis of GTK treeview principle and usage

Analysis of GTK treeview principle and usage

The GtkTreeView component is an advanced component that allows you to create beautiful normal lists or tree-like lists. This construct can contain one or more lines. What about his structure? It adopts the famous MVC (Model View Controller) design framework. That is to say, the data and the display method are separated.

So there are indeed several other independent object structures (objects) in the GtktreeView component.

GtkCellRenderer determines how the data in GtkTreeViewColumn is displayed.

The function of GtkListStore and GtkTreeStore is to reflect the role of the model.

That is, they are used to process and analyze the data to be displayed in the GtkTreeView.

GtkTreeIter is a data structure used to operate on the data in the row in the GtkTreeView component.

GtkTreeSelection is used to handle options.

The effect is as follows

The code is as follows

#include <gtk/gtk.h>

enum
{
  LIST_ITEM = 0,
  N_COLUMNS
};

void init_list(GtkWidget *list)
{

  GtkCellRenderer *renderer;
  GtkTreeViewColumn *column;
  GtkListStore *store;

  renderer = gtk_cell_renderer_text_new ();
  column = gtk_tree_view_column_new_with_attributes("List Items",
       renderer, "text", LIST_ITEM, NULL);
  gtk_tree_view_append_column(GTK_TREE_VIEW(list), column);

  store = gtk_list_store_new(N_COLUMNS, G_TYPE_STRING);

  gtk_tree_view_set_model(GTK_TREE_VIEW(list),
              GTK_TREE_MODEL(store));

  g_object_unref(store);
}

void add_to_list(GtkWidget *list, const gchar *str)
{

  GtkListStore *store;
  GtkTreeIter iter;

  store = GTK_LIST_STORE(gtk_tree_view_get_model
              (GTK_TREE_VIEW(list)));

  gtk_list_store_append(store, &iter);
  gtk_list_store_set(store, &iter, LIST_ITEM, str, -1);
}


void on_changed(GtkWidget *widget, gpointer label)
{

  GtkTreeIter iter;
  GtkTreeModel *model;
  gchar *value;

  if (gtk_tree_selection_get_selected(
        GTK_TREE_SELECTION(widget), &model, &iter))
  {

    gtk_tree_model_get(model, &iter, LIST_ITEM, &value, -1);
    gtk_label_set_text(GTK_LABEL(label), value);
    g_free(value);
  }
}

int main(int argc, char *argv[])
{

  GtkWidget *window;
  GtkWidget *list;

  GtkWidget *vbox;
  GtkWidget *label;
  GtkTreeSelection *selection;

  gtk_init(&argc, &argv);

  window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
  list = gtk_tree_view_new();

  gtk_window_set_title(GTK_WINDOW(window), "List view");
  gtk_window_set_position(GTK_WINDOW(window), GTK_WIN_POS_CENTER); //Set to center.
  gtk_container_set_border_width(GTK_CONTAINER(window), 10);
  gtk_widget_set_size_request(window, 270, 250);

  gtk_tree_view_set_headers_visible(GTK_TREE_VIEW(list), FALSE);

  vbox = gtk_vbox_new(FALSE, 0);

  gtk_box_pack_start(GTK_BOX(vbox), list, TRUE, TRUE, 5);

  label = gtk_label_new("");
  gtk_box_pack_start(GTK_BOX(vbox), label, FALSE, FALSE, 5);

  gtk_container_add(GTK_CONTAINER(window), vbox);

  init_list(list);
  add_to_list(list, "Aliens");
  add_to_list(list, "Leon");
  add_to_list(list, "The Verdict");
  add_to_list(list, "North Face");
  add_to_list(list, "Der Untergang");

  selection = gtk_tree_view_get_selection(GTK_TREE_VIEW(list));

  g_signal_connect(selection, "changed",
           G_CALLBACK(on_changed), label);

  g_signal_connect(G_OBJECT (window), "destroy",
           G_CALLBACK(gtk_main_quit), NULL);

  gtk_widget_show_all(window);

  gtk_main();

  return 0;
}

In the sample code above, we will show you 5 items and arrange them in the GtkTreeView component. We first place a GtkVBox widget in the window. This GtkVBox component contains two components: GtkTreeView and GtkLabel.

list = gtk_tree_view_new();
gtk_tree_view_set_headers_visible(GTK_TREE_VIEW(list), FALSE);

Call the list() function to initialize the component list.

renderer = gtk_cell_renderer_text_new();
 column = gtk_tree_view_column_new_with_attributes("List Items",
     renderer, "text", LIST_ITEM, NULL);
 gtk_tree_view_append_column(GTK_TREE_VIEW(list), column);

In the initialization function, we generate a GtkTreeView with only one column.

store = gtk_list_store_new(N_COLUMNS, G_TYPE_STRING);
 gtk_tree_view_set_model(GTK_TREE_VIEW(list), 
   GTK_TREE_MODEL(store));

Next we created a GtkListStore widget (a model) and bound it to the list widget.

g_object_unref(store);

The model is automatically destroyed to free up memory space.

add_to_list(list, "Aliens");

The above is calling the add_to_list() function to add an option to the list.

store = GTK_LIST_STORE(gtk_tree_view_get_model
(GTK_TREE_VIEW(list)));

gtk_list_store_append(store, &iter);
gtk_list_store_set(store, &iter, LIST_ITEM, str, -1);

In the function add_to_list(), we use the system function gtk_tree_view_get_model() to get the model. We generate a new row and pass the data in the row to the model for processing. This is accomplished with the help of GtkTreeIter.

selection = gtk_tree_view_get_selection(GTK_TREE_VIEW(list));

A GtkTreeSelection does not actually need to be created explicitly. Here, we use the GtkTreeView component to automatically generate it. To help with this, as you can see, is the system function gtk_tree_view_get_selection().

Okay, practice some more.

Add a column

The code is as follows

#include <gtk/gtk.h>

enum
{
  LIST_ITEM = 0,
  LIST_AGE,
  N_COLUMNS
};


void init_list(GtkWidget *list)
{
  //To display data in the view, you must create GtkCellRenderer and GtkTreeViewColumn
  GtkCellRenderer *renderer;
  GtkTreeViewColumn *column;
  GtkListStore *store;
  
  //Create a GtkCellRenderer 
  renderer = gtk_cell_renderer_text_new ();  
  //Create a column with a title and put a renderer in it so that it can display the content column = gtk_tree_view_column_new_with_attributes("List Items", renderer, "text", LIST_ITEM, NULL);  
  //Add columns to gtk_tree_view
  gtk_tree_view_append_column(GTK_TREE_VIEW(list), column);
  
  
  //Create a GtkCellRenderer 
  renderer = gtk_cell_renderer_text_new ();  
  //g_object_set (G_OBJECT (renderer), "xalign", 1.0, NULL); //right-align //Create a column with a title and put the renderer in it so that it can display the content column = gtk_tree_view_column_new_with_attributes("List age", renderer, "text", LIST_AGE, NULL);  
  //Add columns to gtk_tree_view
  gtk_tree_view_append_column(GTK_TREE_VIEW(list), column);
  
  

  store = gtk_list_store_new(N_COLUMNS, G_TYPE_STRING,G_TYPE_INT);
  
  
  //Associate view and model gtk_tree_view_set_model(GTK_TREE_VIEW(list), GTK_TREE_MODEL(store));

  //Submit the data model to the view for management. When the view is destroyed, the data will be destroyed together with it g_object_unref(store);
}

void add_to_list(GtkWidget *list, const gchar *str ,gint age)
{

  GtkListStore *store;
  GtkTreeIter iter;

  store = GTK_LIST_STORE(gtk_tree_view_get_model
              (GTK_TREE_VIEW(list)));

  gtk_list_store_append(store, &iter);
  gtk_list_store_set(store, &iter, LIST_ITEM, str, LIST_AGE, age, -1);
}



void on_changed(GtkWidget *widget, gpointer label)
{

  GtkTreeIter iter;
  GtkTreeModel *model;
  gchar *value;
  
  //Get the GtkTreeIter of a row selected in the treeview
  if (gtk_tree_selection_get_selected( GTK_TREE_SELECTION(widget), &model, &iter))
  {

    gtk_tree_model_get(model, &iter, LIST_ITEM, &value, -1);
    gtk_label_set_text(GTK_LABEL(label), value);
    g_free(value);
  }
}

int main(int argc, char *argv[])
{

  GtkWidget *window;
  GtkWidget *list;

  GtkWidget *vbox;
  GtkWidget *label;
  GtkTreeSelection *selection;

  gtk_init(&argc, &argv);

  window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
  list = gtk_tree_view_new();

  gtk_window_set_title(GTK_WINDOW(window), "List view");
  gtk_window_set_position(GTK_WINDOW(window), GTK_WIN_POS_CENTER);
  gtk_container_set_border_width(GTK_CONTAINER(window), 10);
  gtk_widget_set_size_request(window, 270, 250);

  //Set the visibility state of the title.
  gtk_tree_view_set_headers_visible(GTK_TREE_VIEW(list), TRUE );

  vbox = gtk_box_new (GTK_ORIENTATION_VERTICAL, 0); //gtk_vbox_new (FALSE, 0);

  gtk_box_pack_start(GTK_BOX(vbox), list, TRUE, TRUE, 5);

  label = gtk_label_new("");
  gtk_box_pack_start(GTK_BOX(vbox), label, FALSE, FALSE, 5);

  gtk_container_add(GTK_CONTAINER(window), vbox);

  init_list(list);
  add_to_list(list, "Aliens" ,10 );
  add_to_list(list, "Leon" ,2 );
  add_to_list(list, "The Verdict" ,30 );
  add_to_list(list, "North Face" ,4 );
  add_to_list(list, "Der Untergang",50);

  
  selection = gtk_tree_view_get_selection(GTK_TREE_VIEW(list));

  g_signal_connect(selection, "changed",
           G_CALLBACK(on_changed), label);

  g_signal_connect(G_OBJECT (window), "destroy",
           G_CALLBACK(gtk_main_quit), NULL);

  gtk_widget_show_all(window);

  gtk_main();

  return 0;
}

The above is the full content of this article. I hope it will be helpful for everyone’s study. I also hope that everyone will support 123WORDPRESS.COM.

You may also be interested in:
  • Android TreeView implements a tree-like organizational structure with checkboxes
  • Detailed explanation of Python form (tkinter) tree data (Treeview)
  • Bootstrap treeview tree menu with checkbox and cascading selection function
  • WPF custom TreeView control style to achieve QQ contact list effect
  • Bootstrap treeview dynamically loads data and adds quick search function
  • Android UI: Implementing a multi-level tree list TreeView example
  • Detailed explanation of how to use the JS tree menu component Bootstrap TreeView
  • Detailed explanation of how to use the Bootstrap tree menu plugin TreeView.js
  • A brief analysis of the simple use of BootStrap Treeview

<<:  Detailed explanation of MySQL database triggers

>>:  Differentiate between null value and empty character ('') in MySQL

Recommend

How to improve MySQL Limit query performance

In MySQL database operations, we always hope to a...

Detailed explanation of react setState

Table of contents Is setState synchronous or asyn...

Summary of shell's method for determining whether a variable is empty

How to determine whether a variable is empty in s...

How to set underline in HTML? How to underline text in HTML

Underlining in HTML used to be a matter of enclos...

How to optimize MySQL index function based on Explain keyword

EXPLAIN shows how MySQL uses indexes to process s...

Three Ways to Lock and Unlock User Accounts in Linux

If you already have some kind of password policy ...

Tutorial on deploying nginx+uwsgi in Django project under Centos8

1. Virtual environment virtualenv installation 1....

How to view Linux ssh service information and running status

There are many articles about ssh server configur...

MySQL encryption and decryption examples

MySQL encryption and decryption examples Data enc...

Beginners learn some HTML tags (2)

Beginners can learn HTML by understanding some HT...

Windows 10 installation vmware14 tutorial diagram

Software Download Download software link: https:/...

Practical experience of implementing nginx to forward requests based on URL

Preface Because this is a distributed file system...

A brief discussion on the use and analysis of nofollow tags

Controversy over nofollow There was a dispute bet...

TypeScript interface definition case tutorial

The role of the interface: Interface, in English:...