Gtk.ListStore
Description: Reference for Gtk.ListStore

### Class

# GtkListStore

#### Description [src]

    class Gtk.ListStore : GObject.Object {
      parent: GObject
    }

A list-like data structure that can be used with the GtkTreeView

The `GtkListStore` object is a list model for use with a `GtkTreeView` widget. It implements the `GtkTreeModel` interface, and consequentialy, can use all of the methods available there. It also implements the `GtkTreeSortable` interface so it can be sorted by the view. Finally, it also implements the tree [drag and drop][gtk4-GtkTreeView-drag-and-drop] interfaces.

The `GtkListStore` can accept most GObject types as a column type, though it can’t accept all custom types. Internally, it will keep a copy of data passed in (such as a string or a boxed pointer). Columns that accept `GObject`s are handled a little differently. The `GtkListStore` will keep a reference to the object instead of copying the value. As a result, if the object is modified, it is up to the application writer to call `gtk_tree_model_row_changed()` to emit the `GtkTreeModel`::row_changed signal. This most commonly affects lists with `GdkTexture`s stored.

An example for creating a simple list store:

    enum {
      COLUMN_STRING,
      COLUMN_INT,
      COLUMN_BOOLEAN,
      N_COLUMNS
    };

    {
      GtkListStore *list_store;
      GtkTreePath *path;
      GtkTreeIter iter;
      int i;

      list_store = gtk_list_store_new (N_COLUMNS,
                                       G_TYPE_STRING,
                                       G_TYPE_INT,
                                       G_TYPE_BOOLEAN);

      for (i = 0; i < 10; i++)
        {
          char *some_data;

          some_data = get_some_data (i);

          // Add a new row to the model
          gtk_list_store_append (list_store, &iter);
          gtk_list_store_set (list_store, &iter,
                              COLUMN_STRING, some_data,
                              COLUMN_INT, i,
                              COLUMN_BOOLEAN,  FALSE,
                              -1);

          // As the store will keep a copy of the string internally,
          // we free some_data.
          g_free (some_data);
        }

      // Modify a particular row
      path = gtk_tree_path_new_from_string ("4");
      gtk_tree_model_get_iter (GTK_TREE_MODEL (list_store),
                               &iter,
                               path);
      gtk_tree_path_free (path);
      gtk_list_store_set (list_store, &iter,
                          COLUMN_BOOLEAN, TRUE,
                          -1);
    }

# Performance Considerations

Internally, the `GtkListStore` was originally implemented with a linked list with a tail pointer. As a result, it was fast at data insertion and deletion, and not fast at random data access. The `GtkListStore` sets the `GTK_TREE_MODEL_ITERS_PERSIST` flag, which means that `GtkTreeIter`s can be cached while the row exists. Thus, if access to a particular row is needed often and your code is expected to run on older versions of GTK, it is worth keeping the iter around.

# Atomic Operations

It is important to note that only the methods `gtk_list_store_insert_with_values()` and `gtk_list_store_insert_with_valuesv()` are atomic, in the sense that the row is being appended to the store and the values filled in in a single operation with regard to `GtkTreeModel` signaling. In contrast, using e.g. `gtk_list_store_append()` and then `gtk_list_store_set()` will first create a row, which triggers the `GtkTreeModel`::row-inserted signal on `GtkListStore`. The row, however, is still empty, and any signal handler connecting to `GtkTreeModel`::row-inserted on this particular store should be prepared for the situation that the row might be empty. This is especially important if you are wrapping the `GtkListStore` inside a `GtkTreeModel`Filter and are using a `GtkTreeModel`FilterVisibleFunc. Using any of the non-atomic operations to append rows to the `GtkListStore` will cause the `GtkTreeModel`FilterVisibleFunc to be visited with an empty row first; the function must be prepared for that.

# GtkListStore as GtkBuildable

The GtkListStore implementation of the GtkBuildable interface allows to specify the model columns with a element that may contain multiple elements, each specifying one model column. The “type” attribute specifies the data type for the column.

Additionally, it is possible to specify content for the list store in the UI definition, with the element. It can contain multiple elements, each specifying to content for one row of the list model. Inside a , the elements specify the content for individual cells.

Note that it is probably more common to define your models in the code, and one might consider it a layering violation to specify the content of a list store in a UI definition, data, not presentation, and common wisdom is to separate the two, as far as possible.

An example of a UI Definition fragment for a list store:

    <object class="GtkListStore">
      <columns>
        <column type="gchararray"/>
        <column type="gchararray"/>
        <column type="gint"/>
      </columns>
      <data>
        <row>
          <col id="0">John</col>
          <col id="1">Doe</col>
          <col id="2">25</col>
        </row>
        <row>
          <col id="0">Johan</col>
          <col id="1">Dahlin</col>
          <col id="2">50</col>
        </row>
      </data>
    </object>

#### Ancestors

- GObject

#### Implements

- GtkBuildable
- GtkTreeDragDest
- GtkTreeDragSource
- GtkTreeModel
- GtkTreeSortable

#### Constructors

###### gtk_list_store_new

Creates a new list store as with `n_columns` columns each of the types passed in. Note that only types derived from standard GObject fundamental types are supported.

###### gtk_list_store_newv

Non-vararg creation function. Used primarily by language bindings.

#### Instance methods

###### gtk_list_store_append

Appends a new row to `list_store`. `iter` will be changed to point to this new row. The row will be empty after this function is called. To fill in values, you need to call `gtk_list_store_set()` or `gtk_list_store_set_value()`.

###### gtk_list_store_clear

Removes all rows from the list store.

###### gtk_list_store_insert

Creates a new row at `position`. `iter` will be changed to point to this new row. If `position` is -1 or is larger than the number of rows on the list, then the new row will be appended to the list. The row will be empty after this function is called. To fill in values, you need to call `gtk_list_store_set()` or `gtk_list_store_set_value()`.

###### gtk_list_store_insert_after

Inserts a new row after `sibling`. If `sibling` is `NULL`, then the row will be prepended to the beginning of the list. `iter` will be changed to point to this new row. The row will be empty after this function is called. To fill in values, you need to call `gtk_list_store_set()` or `gtk_list_store_set_value()`.

###### gtk_list_store_insert_before

Inserts a new row before `sibling`. If `sibling` is `NULL`, then the row will be appended to the end of the list. `iter` will be changed to point to this new row. The row will be empty after this function is called. To fill in values, you need to call `gtk_list_store_set()` or `gtk_list_store_set_value()`.

###### gtk_list_store_insert_with_values

Creates a new row at `position`. `iter` will be changed to point to this new row. If `position` is -1, or larger than the number of rows in the list, then the new row will be appended to the list. The row will be filled with the values given to this function.

###### gtk_list_store_insert_with_valuesv

A variant of `gtk_list_store_insert_with_values()` which takes the columns and values as two arrays, instead of varargs.

###### gtk_list_store_iter_is_valid

Checks if the given iter is a valid iter for this `GtkListStore`.

###### gtk_list_store_move_after

Moves `iter` in `store` to the position after `position`. Note that this function only works with unsorted stores. If `position` is `NULL`, `iter` will be moved to the start of the list.

###### gtk_list_store_move_before

Moves `iter` in `store` to the position before `position`. Note that this function only works with unsorted stores. If `position` is `NULL`, `iter` will be moved to the end of the list.

###### gtk_list_store_prepend

Prepends a new row to `list_store`. `iter` will be changed to point to this new row. The row will be empty after this function is called. To fill in values, you need to call `gtk_list_store_set()` or `gtk_list_store_set_value()`.

###### gtk_list_store_remove

Removes the given row from the list store. After being removed, `iter` is set to be the next valid row, or invalidated if it pointed to the last row in `list_store`.

###### gtk_list_store_reorder

Reorders `store` to follow the order indicated by `new_order`. Note that this function only works with unsorted stores.

###### gtk_list_store_set

Sets the value of one or more cells in the row referenced by `iter`. The variable argument list should contain integer column numbers, each column number followed by the value to be set. The list is terminated by a -1. For example, to set column 0 with type `G_TYPE_STRING` to “Foo”, you would write `gtk_list_store_set (store, iter, 0, "Foo", -1)`.

###### gtk_list_store_set_column_types

This function is meant primarily for `GObject`s that inherit from `GtkListStore`, and should only be used when constructing a new `GtkListStore`. It will not function after a row has been added, or a method on the `GtkTreeModel` interface is called.

###### gtk_list_store_set_valist

See `gtk_list_store_set()`; this version takes a va_list for use by language bindings.

###### gtk_list_store_set_value

Sets the data in the cell specified by `iter` and `column`. The type of `value` must be convertible to the type of the column.

###### gtk_list_store_set_valuesv

A variant of `gtk_list_store_set_valist()` which takes the columns and values as two arrays, instead of varargs. This function is mainly intended for language-bindings and in case the number of columns to change is not known until run-time.

###### gtk_list_store_swap

Swaps `a` and `b` in `store`. Note that this function only works with unsorted stores.

##### Methods inherited from GtkBuildable (1)

###### gtk_buildable_get_buildable_id

Gets the ID of the `buildable` object.

##### Methods inherited from GtkTreeDragDest (2)

###### gtk_tree_drag_dest_drag_data_received

Asks the `GtkTreeDragDest` to insert a row before the path `dest`, deriving the contents of the row from `value`. If `dest` is outside the tree so that inserting before it is impossible, `FALSE` will be returned. Also, `FALSE` may be returned if the new row is not created for some model-specific reason. Should robustly handle a `dest` no longer found in the model!

###### gtk_tree_drag_dest_row_drop_possible

Determines whether a drop is possible before the given `dest_path`, at the same depth as `dest_path`. i.e., can we drop the data in `value` at that location. `dest_path` does not have to exist; the return value will almost certainly be `FALSE` if the parent of `dest_path` doesn’t exist, though.

##### Methods inherited from GtkTreeDragSource (3)

###### gtk_tree_drag_source_drag_data_delete

Asks the `GtkTreeDragSource` to delete the row at `path`, because it was moved somewhere else via drag-and-drop. Returns `FALSE` if the deletion fails because `path` no longer exists, or for some model-specific reason. Should robustly handle a `path` no longer found in the model!

###### gtk_tree_drag_source_drag_data_get

Asks the `GtkTreeDragSource` to return a `GdkContentProvider` representing the row at `path`. Should robustly handle a `path` no longer found in the model!

###### gtk_tree_drag_source_row_draggable

Asks the `GtkTreeDragSource` whether a particular row can be used as the source of a DND operation. If the source doesn’t implement this interface, the row is assumed draggable.

##### Methods inherited from GtkTreeModel (28)

###### gtk_tree_model_filter_new

Creates a new `GtkTreeModel`, with `child_model` as the child_model and `root` as the virtual root.

###### gtk_tree_model_foreach

Calls `func` on each node in model in a depth-first fashion.

###### gtk_tree_model_get

Gets the value of one or more cells in the row referenced by `iter`.

###### gtk_tree_model_get_column_type

Returns the type of the column.

###### gtk_tree_model_get_flags

Returns a set of flags supported by this interface.

###### gtk_tree_model_get_iter

Sets `iter` to a valid iterator pointing to `path`.

###### gtk_tree_model_get_iter_first

Initializes `iter` with the first iterator in the tree (the one at the path “0”).

###### gtk_tree_model_get_iter_from_string

Sets `iter` to a valid iterator pointing to `path_string`, if it exists.

###### gtk_tree_model_get_n_columns

Returns the number of columns supported by `tree_model`.

###### gtk_tree_model_get_path

Returns a newly-created `GtkTreePath` referenced by `iter`.

###### gtk_tree_model_get_string_from_iter

Generates a string representation of the iter.

###### gtk_tree_model_get_valist

Gets the value of one or more cells in the row referenced by `iter`.

###### gtk_tree_model_get_value

Initializes and sets `value` to that at `column`.

###### gtk_tree_model_iter_children

Sets `iter` to point to the first child of `parent`.

###### gtk_tree_model_iter_has_child

Returns `TRUE` if `iter` has children, `FALSE` otherwise.

###### gtk_tree_model_iter_n_children

Returns the number of children that `iter` has.

###### gtk_tree_model_iter_next

Sets `iter` to point to the node following it at the current level.

###### gtk_tree_model_iter_nth_child

Sets `iter` to be the child of `parent`, using the given index.

###### gtk_tree_model_iter_parent

Sets `iter` to be the parent of `child`.

###### gtk_tree_model_iter_previous

Sets `iter` to point to the previous node at the current level.

###### gtk_tree_model_ref_node

Lets the tree ref the node.

###### gtk_tree_model_row_changed

Emits the ::row-changed signal on `tree_model`.

###### gtk_tree_model_row_deleted

Emits the ::row-deleted signal on `tree_model`.

##### Methods inherited from GtkTreeSortable (6)

###### gtk_tree_sortable_get_sort_column_id

Fills in `sort_column_id` and `order` with the current sort column and the order. It returns `TRUE` unless the `sort_column_id` is `GTK_TREE_SORTABLE_DEFAULT_SORT_COLUMN_ID` or `GTK_TREE_SORTABLE_UNSORTED_SORT_COLUMN_ID`.

###### gtk_tree_sortable_has_default_sort_func

Returns `TRUE` if the model has a default sort function. This is used primarily by GtkTreeViewColumns in order to determine if a model can go back to the default state, or not.

###### gtk_tree_sortable_set_default_sort_func

Sets the default comparison function used when sorting to be `sort_func`. If the current sort column id of `sortable` is `GTK_TREE_SORTABLE_DEFAULT_SORT_COLUMN_ID`, then the model will sort using this function.

###### gtk_tree_sortable_set_sort_column_id

Sets the current sort column to be `sort_column_id`. The `sortable` will resort itself to reflect this change, after emitting a `GtkTreeSortable::sort-column-changed` signal. `sort_column_id` may either be a regular column id, or one of the following special values:

###### gtk_tree_sortable_set_sort_func

Sets the comparison function used when sorting to be `sort_func`. If the current sort column id of `sortable` is the same as `sort_column_id`, then the model will sort using this function.

###### gtk_tree_sortable_sort_column_changed

Emits a `GtkTreeSortable::sort-column-changed` signal on `sortable`.

#### Class structure

    struct GtkListStoreClass {
      GObjectClass parent_class;

    }

###### Class members

`parent_class` |
    GObjectClass
| No description available.