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