5

I have a treeview that is populated from a treemodel.

I would like to add a colum to the treeview. Is it possible to draw the data for that column from a separate treemodel or can I append at runtime a column to the existing treemodel?

gpoo
  • 8,408
  • 3
  • 38
  • 53
miller the gorilla
  • 860
  • 1
  • 7
  • 19

3 Answers3

4

You can append as many columns to the tree view as you need, without the limit of the columns of the model. If the data you need are not present in the model, you can set a callback for a column:

import gtk


def inIta(col, cell, model, iter, mymodel):
    s = model.get_string_from_iter(iter)
    niter = mymodel.get_iter_from_string(s)
    obj = mymodel.get_value(niter, 0)
    cell.set_property('text', obj)


model = gtk.ListStore(str)
model2 = gtk.ListStore(str)
view = gtk.TreeView(model)
rend1 = gtk.CellRendererText()
col1 = gtk.TreeViewColumn('hello', rend1, text=0)
view.append_column(col1)
rend2 = gtk.CellRendererText()
col2 = gtk.TreeViewColumn('ciao', rend2)
col2.set_cell_data_func(rend2, inIta, model2)
view.append_column(col2)

model.append(['hello world'])
model2.append(['ciao mondo'])

win = gtk.Window()
win.connect('delete_event', gtk.main_quit)
win.add(view)
win.show_all()
gtk.main()
mg.
  • 7,822
  • 1
  • 26
  • 30
  • Thanks, that's just what I need. Shame you can't define a one to many and many to one relationship between the model and the treeview natively in gtk. I would have thought this would make it more useful for representing database information. – miller the gorilla Jan 19 '12 at 19:47
  • I think there are libraries built on top of gtk, [kiwi](http://www.async.com.br/projects/kiwi/) is one, but I think there is another one more specific to build interfaces on top of databases. – mg. Jan 19 '12 at 20:13
0

To answer the question in the title: No, you can't add columns to a GtkTreeModel after it's been created.

TheAmigo
  • 1,032
  • 1
  • 10
  • 29
0

In a gtk.TreeView object there's an append_column method, so yes, you can programmatically add a column to a gtk.TreeView.

However, I'm not aware of any method for adding a new column to an existing model or using multiple models for the same gtk.TreeView. Anyway, I guess you can create a new model with an extra column, copy the contents of the previous one and update the tree view to use the new model.

jcollado
  • 39,419
  • 8
  • 102
  • 133