GTK+ 3.0: How to use a Gtk.TreeStore with custom model items?
Asked Answered
S

1

10

I'm trying to develop a GTK application in Python and I'm really stuck with the correct usage of a gtk.TreeStore. My main problem: I've already parsed some JSON and I have my own data structure which ist basically a Python list and two kinds of objects: One represents a collection of items (collections can't be nested) and one for representing items (which might appear in the list as well as in a collection).

I'm already familiar with the basic usage of a TreeStore and managed to get items correctly rendered on screen. I don't know how to deal with the fact that a treestore is only capable of storing gobject types (at this point I'm not sure because I don't know much about the gobject type system). The documentation for C lists the following, (except PixBuf) basic types which can be inserted and are automagically mapped to Python data types:

As an example, gtk_tree_store_new (3, G_TYPE_INT, G_TYPE_STRING, GDK_TYPE_PIXBUF); will create a new GtkTreeStore with three columns, of type int, string and GdkPixbuf respectively.

Furthermore it says you may insert any GType. The link from the documentation directly points at this paragraph:

A numerical value which represents the unique identifier of a registered type.

My research of the topic ends here and Google finds mostly GTK 2.x tutorials and nothing about inserting other data types except str and int etc.
Questions:

  • Is it possible to implement a new GType (or any other interface that will make insert custom data in a treestore possible) and how to do it?
    I already tried deriving from GObject but it didn't help.

  • How can I get rid of keeping two data structures at the same time?
    Namely my parsing result and the duplicate information in a Treestore.

  • How is it possible to deal with mixed content?
    In my case I have collections and items with different additional information (which are mirrored in the treeview as nodes with or without children).

If above questions are resolved, I also get rid of the problem when handling selections: it is difficult to match a simple type like str or int to match an item I inserted before. Such attribute has to be a key and still you would to search the list with parsed results which is quit ineffective.

Thank you in advance!

Additional information not directly related to the question:


I thought it could be a feasible challenge to implement a custom TreeModel until I read this in a tutorial for GTK 2:

However, all this comes at a cost: you are unlikely to write a useful custom model in less than a thousand lines, unless you strip all newline characters. Writing a custom model is not as difficult as it might sound though, and it may well be worth the effort, not least because it will result in much saner code if you have a lot of data to keep track of.

Is this still valid?


I just came across http://www.pygtk.org/articles/subclassing-gobject/sub-classing-gobject-in-python.htm Can this be helpful? As many resoucres it's for PyGTK 2.0. deprecated.

Sharynshashlik answered 24/6, 2012 at 15:38 Comment(3)
Note that a thousand lines of C is more like 200 lines of Python.Aquamanile
I have implemented a custom Treemodel with less than 100 lines. I think that the main issue may be performance, because the original c functions are replaced by python functions.Headley
There is this related question. In its comment, there is a link to an example of python 3 Treemodel interface custom implemented. #32612320Lamed
S
17

Problem solved! For other people coming across the same problem, I will pile up a few helpful resources and my example code. It's okay if you know how to do it, but it's really nowhere documented.

Complete example code for having a TreeView filled with Persons and printing the selected person on a button click:

from gi.repository import Gtk
from gi.repository import GObject

class Person (GObject.GObject):
    name = GObject.property(type=str)
    age = GObject.property(type=int)
    gender = GObject.property(type=bool, default=True)

    def __init__(self):
        GObject.GObject.__init__(self)

    def __repr__(self):
        s = None
        if self.get_property("gender"): s = "m"
        else: s = "f"
        return "%s, %s, %i" % (self.get_property("name"), s, self.get_property("age"))

class MyApplication (Gtk.Window):

    def __init__(self, *args, **kwargs):
        Gtk.Window.__init__(self, *args, **kwargs)
        self.set_title("Tree Display")
        self.set_size_request(400, 400)
        self.connect("destroy", Gtk.main_quit)
        self.create_widgets()
        self.insert_rows()
        self.show_all()

    def create_widgets(self):
        self.treestore = Gtk.TreeStore(Person.__gtype__)
        self.treeview = Gtk.TreeView()
        self.treeview.set_model(self.treestore)
        column = Gtk.TreeViewColumn("Person")

        cell = Gtk.CellRendererText()
        column.pack_start(cell, True)

        column.set_cell_data_func(cell, self.get_name)

        self.treeview.append_column(column)
        vbox = Gtk.VBox()
        self.add(vbox)
        vbox.pack_start(self.treeview, True, True, 0)

        button = Gtk.Button("Retrieve element")
        button.connect("clicked", self.retrieve_element)
        vbox.pack_start(button, False, False, 5)

    def get_name(self, column, cell, model, iter, data):
        cell.set_property('text', self.treestore.get_value(iter, 0).name)

    def insert_rows(self):
        for name, age, gender in [("Tom", 19, True), ("Anna", 35, False)]:
            p = Person()
            p.name = name
            p.age = age
            p.gender = gender
            self.treestore.append(None, (p,))

    def retrieve_element(self, widget):
        model, treeiter = self.treeview.get_selection().get_selected()
        if treeiter:
            print "You selected", model[treeiter][0]

if __name__ == "__main__":
    GObject.type_register(Person)
    MyApplication()
    Gtk.main()
Sharynshashlik answered 24/6, 2012 at 19:35 Comment(3)
For python3 users: change the print statement and add the lines import gi and gi.require_version("Gtk","3.0") and you're good to go.Plier
Many thanks for this answer, it allowed me to get my tree to include the Human-readable part along with database IDs, I know this is a really old answer, buy could you please explain the relationship between the name/age/gender GObject.Property in Person and the object member variables set later in insert_rows() ?Archway
@Archway yes, it's been a while, and I don't remember all the details. From the looks of it, probably the constructor of GObjectdoes some magic behind the scenes to turn the property definitions in the class body into fields. Or maybe GObject provides a suitable getattr method. I'm sorry I can't dive into deeper detail 🤷Sharynshashlik

© 2022 - 2024 — McMap. All rights reserved.