Gtk.ListStore


Gtk.ListStore — A list-like data structure that can be used with the Gtk.TreeView

Object Hierarchy:

    GObject
    ╰── Gtk.ListStore

See also:

Gtk.TreeModel, Gtk.TreeStore


Functions:

  • new (types:list) -> Gtk.ListStore
  • set_column_types (self, types:list)
  • set (self, iter:Gtk.TreeIter, columns:list, values:list)
  • set_value (self, iter:Gtk.TreeIter, column:int, value:GObject.Value)
  • remove (self, iter:Gtk.TreeIter) -> bool
  • insert (self, position:int) -> iter:Gtk.TreeIter
  • insert_before (self, sibling:Gtk.TreeIter=None) -> iter:Gtk.TreeIter
  • insert_after (self, sibling:Gtk.TreeIter=None) -> iter:Gtk.TreeIter
  • insert_with_valuesv (self, position:int, columns:list, values:list) -> iter:Gtk.TreeIter
  • prepend (self) -> iter:Gtk.TreeIter
  • append (self) -> iter:Gtk.TreeIter
  • clear (self)
  • iter_is_valid (self, iter:Gtk.TreeIter) -> bool
  • reorder (self, new_order:list)
  • swap (self, a:Gtk.TreeIter, b:Gtk.TreeIter)
  • move_before (self, iter:Gtk.TreeIter, position:Gtk.TreeIter=None)
  • move_after (self, iter:Gtk.TreeIter, position:Gtk.TreeIter=None)

Description:

The Gtk.ListStore object is a list model for use with a Gtk.TreeView widget. It implements the Gtk.TreeModel interface, and consequentialy, can use all of the methods available there. It also implements the Gtk.TreeSortable interface so it can be sorted by the view.

Finally, it also implements the tree drag and drop interfaces.

The Gtk.ListStore 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 GObjects are handled a little differently. The Gtk.ListStore 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.TreeModel::row_changed() to emit the Gdk.Pixbufs stored.

An example for creating a simple list store:

    ...

    liststore = Gtk.ListStore(str, int)
    for data in [["Oranges", 5], ["Apples", 3], ["Bananas", 2], ["Tomatoes", 4], ["Cucumber", 1]]:
        liststore.append(data)

    ...
}

Performance Considerations

Internally, the Gtk.ListStore was implemented with a linked list with a tail pointer prior to GTK+ 2.6. As a result, it was fast at data insertion and deletion, and not fast at random data access. The Gtk.ListStore sets the #GTK_TREE_MODEL_ITERS_PERSIST flag, which means that GtkTreeIters 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.ListStore::insert_with_values() and Gtk.ListStore::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 Gtk.TreeModel signaling.

In contrast, using e.g. Gtk.ListStore::append() and then Gtk.ListStore::set() will first create a row, which triggers the Gtk.TreeModel::row-inserted signal on Gtk.ListStore. The row, however, is still empty, and any signal handler connecting to Gtk.TreeModel::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 Gtk.TreeModelFilter and are using a Gtk.TreeModelFilterVisibleFunc. Using any of the non-atomic operations to append rows to the Gtk.ListStore will cause the Gtk.TreeModelFilterVisibleFunc to be visited with an empty row first; the function must be prepared for that.

Gtk.ListStore as GtkBuildable

The Gtk.ListStore 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>

Function Details:

new()

new (types:list) -> Gtk.ListStore

Creates a new list store as with n_columns columns each of the types passedin. Note that only types derived from standard GObject fundamental typesare supported. As an example, Gtk.ListStore:new (3, G_TYPE_INT, G_TYPE_STRING,GDK_TYPE_PIXBUF); will create a new Gtk.ListStore with three columns, of typeint, string and GdkPixbuf respectively.

  • Returns: a new Gtk.ListStore

set_column_types()

set_column_types (self, types:list)

This function is meant primarily for GObjects that inherit from Gtk.ListStore,and should only be used when constructing a new Gtk.ListStore. It will notfunction after a row has been added, or a method on the Gtk.TreeModelinterface is called.


set()

set (self, iter:Gtk.TreeIter, columns:list, values:list)

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 typeG_TYPE_STRING to “Foo”, you would write Gtk.ListStore:set (store, iter,0, "Foo", -1). The value will be referenced by the store if it is a G_TYPE_OBJECT, and itwill be copied if it is a G_TYPE_STRING or G_TYPE_BOXED.


set_value()

set_value (self, iter:Gtk.TreeIter, column:int, value:GObject.Value)

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


remove()

remove (self, iter:Gtk.TreeIter) -> bool

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.

  • Returns: True if iteris valid, FALSE if not.

insert()

insert (self, position:int) -> iter:Gtk.TreeIter

Creates a new row at position. iter will be changed to point to this newrow. 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 afterthis function is called. To fill in values, you need to callGtk.ListStore:set() or Gtk.ListStore:set_value().


insert_before()

insert_before (self, sibling:Gtk.TreeIter=None) -> iter:Gtk.TreeIter

Inserts a new row before sibling. If sibling is None, 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.ListStore:set() or Gtk.ListStore:set_value().


insert_after()

insert_after (self, sibling:Gtk.TreeIter=None) -> iter:Gtk.TreeIter

Inserts a new row after sibling. If sibling is None, then the row will beprepended to the beginning of the list. iter will be changed to point tothis new row. The row will be empty after this function is called. To fillin values, you need to call Gtk.ListStore:set() or Gtk.ListStore:set_value().


insert_with_valuesv()

insert_with_valuesv (self, position:int, columns:list, values:list) -> iter:Gtk.TreeIter

A variant of Gtk.ListStore:insert_with_values() whichtakes the columns and values as two arrays, instead ofvarargs. This function is mainly intended for language-bindings.

  • Since: 2.6

prepend()

prepend (self) -> iter:Gtk.TreeIter

Prepends a new row to list_store. iter will be changed to point to this newrow. The row will be empty after this function is called. To fill invalues, you need to call Gtk.ListStore:set() or Gtk.ListStore:set_value().


append()

append (self) -> iter:Gtk.TreeIter

Appends a new row to list_store. iter will be changed to point to this newrow. The row will be empty after this function is called. To fill invalues, you need to call Gtk.ListStore:set() or Gtk.ListStore:set_value().


clear()

clear (self)

Removes all rows from the list store.


iter_is_valid()

iter_is_valid (self, iter:Gtk.TreeIter) -> bool

This function is slow. Only use it for debugging and/or testingpurposes. Checks if the given iter is a valid iter for this Gtk.ListStore.

  • Returns: True if the iter is valid, FALSE if the iter is invalid.

  • Since: 2.2


reorder()

reorder (self, new_order:list)

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

  • Since: 2.2

swap()

swap (self, a:Gtk.TreeIter, b:Gtk.TreeIter)

Swaps a and b in store. Note that this function only works withunsorted stores.

  • Since: 2.2

move_before()

move_before (self, iter:Gtk.TreeIter, position:Gtk.TreeIter=None)

Moves iter in store to the position before position. Note that thisfunction only works with unsorted stores. If position is None, iterwill be moved to the end of the list.

  • Since: 2.2

move_after()

move_after (self, iter:Gtk.TreeIter, position:Gtk.TreeIter=None)

Moves iter in store to the position after position. Note that thisfunction only works with unsorted stores. If position is None, iterwill be moved to the start of the list.

  • Since: 2.2

Example:

import gi
gi.require_version('Gtk', '3.0')
from gi.repository import Gtk

class ListStore(Gtk.Window):
    def __init__(self):
        Gtk.Window.__init__(self)
        self.connect("destroy", Gtk.main_quit)

        liststore = Gtk.ListStore(str, int)
        liststore.append(["Oranges", 5])
        liststore.append(["Apples", 3])
        liststore.append(["Bananas", 2])
        liststore.append(["Tomatoes", 4])
        liststore.append(["Cucumber", 1])

        treeview = Gtk.TreeView()
        treeview.set_model(liststore)
        self.add(treeview)

        cellrenderertext = Gtk.CellRendererText()

        treeviewcolumn = Gtk.TreeViewColumn("Item")
        treeview.append_column(treeviewcolumn)
        treeviewcolumn.pack_start(cellrenderertext, True)
        treeviewcolumn.add_attribute(cellrenderertext, "text", 0)

        treeviewcolumn = Gtk.TreeViewColumn("Quantity")
        treeview.append_column(treeviewcolumn)
        treeviewcolumn.pack_start(cellrenderertext, True)
        treeviewcolumn.add_attribute(cellrenderertext, "text", 1)

window = ListStore()
window.show_all()

Gtk.main()

results matching ""

    No results matching ""