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

ReactJs Basics Tutorial - Essential Edition

Table of contents 1. Introduction to ReactJS 2. U...

In-depth analysis of the Tomcat server of Centos 7 system

Table of contents 1. The origin of tomcat 1. Tomc...

Problems encountered when uploading images using axios in Vue

Table of contents What is FormData? A practical e...

Example of how to implement local fuzzy search function in front-end JavaScript

Table of contents 1. Project Prospects 2. Knowled...

Detailed explanation of 4 common data sources in Spark SQL

Generic load/write methods Manually specify optio...

Detailed explanation of scheduled tasks for ordinary users in Linux

Preface Ordinary users define crontab scheduled t...

Understanding and usage scenarios of ES6 extension operators

Table of contents 1. Replace the apply method, ge...

A brief discussion on the principle of shallow entry and deep exit of MySQL

Table of contents 1. Overview of the page 2. Infi...

A complete tutorial on installing Ubuntu 20.04 using VMware virtual machine

Ubuntu is a relatively popular Linux desktop syst...

How to set the text in the select drop-down menu to scroll left and right

I want to use the marquee tag to set the font scro...

How to mount a data disk on Tencent Cloud Server Centos

First, check whether the hard disk device has a d...

CSS3 mobile vw+rem method to achieve responsive layout without relying on JS

1. Introduction (1) Introduction to vw/vh Before ...

How to apply TypeScript classes in Vue projects

Table of contents 1. Introduction 2. Use 1. @Comp...

How to remove the dividing line of a web page table

<br />How to remove the dividing lines of a ...