Reputation: 920
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?
Upvotes: 5
Views: 2163
Reputation: 1072
To answer the question in the title: No, you can't add columns to a GtkTreeModel after it's been created.
Upvotes: 0
Reputation: 8012
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()
Upvotes: 4
Reputation: 40374
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.
Upvotes: 0