Gtk.TreeModelSort


Gtk.TreeModelSort — A Gtk.TreeModel which makes an underlying tree model sortable

Object Hierarchy:

    GObject
    ╰── Gtk.TreeModelSort

See also:

Gtk.TreeModel, Gtk.ListStore, Gtk.TreeStore, Gtk.TreeSortable, Gtk.TreeModelFilter


Functions:

Description:

The Gtk.TreeModelSort is a model which implements the Gtk.TreeSortable interface. It does not hold any data itself, but rather is created with a child model and proxies its data. It has identical column types to this child model, and the changes in the child are propagated. The primary purpose of this model is to provide a way to sort a different model without modifying it. Note that the sort function used by Gtk.TreeModelSort is not guaranteed to be stable.

The use of this is best demonstrated through an example. In the following sample code we create two Gtk.TreeView widgets each with a view of the same data. As the model is wrapped here by a Gtk.TreeModelSort, the two GtkTreeViews can each sort their view of the data without affecting the other. By contrast, if we simply put the same model in each widget, then sorting the first would sort the second.

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

class TestWindow(Gtk.Window):
    def __init__(self):
        Gtk.Window.__init__(self)
        self.set_title("Test Window")
        self.connect("destroy", Gtk.main_quit)
        self.set_size_request(300, 300)

        box = Gtk.VBox()
        self.add(box)

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

        treemodelsort1 = Gtk.TreeModelSort(list_store)
        treemodelsort1.set_sort_column_id(0, Gtk.SortType.ASCENDING)

        treemodelsort2 = Gtk.TreeModelSort(list_store)
        treemodelsort2.set_sort_column_id(1, Gtk.SortType.DESCENDING)

        treeview1 = Gtk.TreeView()
        treeview1.set_model(treemodelsort1)
        box.add(treeview1)

        treeview2 = Gtk.TreeView()
        treeview2.set_model(treemodelsort2)
        box.add(treeview2)

        self.add_tree_column(treeview1)
        self.add_tree_column(treeview2)

    def add_tree_column(self, treeview):
        celltext = Gtk.CellRendererText()

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

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

window = TestWindow()
window.show_all()

Gtk.main()

To demonstrate how to access the underlying child model from the sort model, the next example will be a callback for the Gtk.TreeSelection from COLUMN_1 of the model. We then modify the string, find the same selected row on the child model, and change the row there.

Accessing the child model of in a selection changed callback:
import gi
gi.require_version('Gtk', '3.0')
from gi.repository import Gtk

class TreeModelSort(Gtk.Window):
    def __init__(self):
        Gtk.Window.__init__(self)
        self.set_default_size(200, -1)
        self.connect("destroy", Gtk.main_quit)

        liststore = Gtk.ListStore(str)
        liststore.append(["Mark"])
        liststore.append(["Chris"])
        liststore.append(["Tim"])
        liststore.append(["David"])
        liststore.append(["Keith"])

        treemodelsort = Gtk.TreeModelSort(liststore)
        treemodelsort.set_sort_column_id(0, Gtk.SortType.ASCENDING)

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

        cellrenderertext = Gtk.CellRendererText()
        treeviewcolumn = Gtk.TreeViewColumn("Name", cellrenderertext, text=0)
        treeview.append_column(treeviewcolumn)        

        treeview.connect('cursor-changed', self.on_select)

    def on_select(self, tree):
        # Get the current selected row and the model.
        model, iter = tree.get_selection().get_selected()        

        # Look up the current value on the selected row and get
        # a new value to change it to.
        data = model.get_value(iter, 0)

        # Get an iterator on the child model, instead of the sort model.
        child_iter = model.convert_iter_to_child_iter(iter)

        # Get the child model and change the value of the row. In this
        # example, the child model is a GtkListStore. It could be any other
        # type of model, though.        
        store = model.get_model()
        store.set_value(child_iter, 0, data + '*')     

window = TreeModelSort()
window.show_all()

Gtk.main()

Function Details:

get_model()

get_model (self) -> Gtk.TreeModel

Returns the model the Gtk.TreeModelSort is sorting.

  • Returns: the "child model" being sorted.

convert_child_path_to_path()

convert_child_path_to_path (self, child_path:Gtk.TreePath) -> Gtk.TreePath

Converts child_path to a path relative to tree_model_sort. That is,child_path points to a path in the child model. The returned path willpoint to the same row in the sorted model. If child_path isn’t a valid path on the child model, then None is returned.

  • Returns: A newly allocated Gtk.TreePath, or None

convert_child_iter_to_iter()

convert_child_iter_to_iter (self, child_iter:Gtk.TreeIter) -> bool, sort_iter:Gtk.TreeIter

Sets sort_iter to point to the row in tree_model_sort that corresponds tothe row pointed at by child_iter. If sort_iter was not set, FALSEis returned. Note: a boolean is only returned since 2.14.

  • Returns: True, if sort_iterwas set, i.e. if sort_iteris avalid iterator pointer to a visible row in the child model.

convert_path_to_child_path()

convert_path_to_child_path (self, sorted_path:Gtk.TreePath) -> Gtk.TreePath

Converts sorted_path to a path on the child model of tree_model_sort. That is, sorted_path points to a location in tree_model_sort. The returned path will point to the same location in the model not being sorted. If sorted_path does not point to a location in the child model, None is returned.

  • Returns: A newly allocated Gtk.TreePath, or None

convert_iter_to_child_iter()

convert_iter_to_child_iter (self, sorted_iter:Gtk.TreeIter) -> child_iter:Gtk.TreeIter

Sets child_iter to point to the row pointed to by sorted_iter.


reset_default_sort_func()

reset_default_sort_func (self)

This resets the default sort function to be in the “unsorted” state. Thatis, it is in the same order as the child model. It will re-sort the modelto be in the same order as the child model only if the Gtk.TreeModelSortis in “unsorted” state.


clear_cache()

clear_cache (self)

This function should almost never be called. It clears the tree_model_sortof any cached iterators that haven’t been reffed with`Gtk.TreeModel:ref_node()`. This might be useful if the child model beingsorted is static (and doesn’t change often) and there has been a lot ofunreffed access to nodes. As a side effect of this function, all unreffediters will be invalid.


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.TreeModelSort.

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

  • Since: 2.2


Example:

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

class TreeModelSort(Gtk.Window):
    def __init__(self):
        Gtk.Window.__init__(self)
        self.set_default_size(200, -1)
        self.connect("destroy", Gtk.main_quit)

        liststore = Gtk.ListStore(str)
        liststore.append(["Mark"])
        liststore.append(["Chris"])
        liststore.append(["Tim"])
        liststore.append(["David"])
        liststore.append(["Keith"])

        treemodelsort = Gtk.TreeModelSort(liststore)
        treemodelsort.set_sort_column_id(0, Gtk.SortType.ASCENDING)

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

        cellrenderertext = Gtk.CellRendererText()
        treeviewcolumn = Gtk.TreeViewColumn("Name", cellrenderertext, text=0)
        treeview.append_column(treeviewcolumn)

window = TreeModelSort()
window.show_all()

Gtk.main()

results matching ""

    No results matching ""