Reputation: 109
Is there any simple way how to make a Gtk.Treeview updating its columns on edit?
I build the Treeview based on a Gtk.ListStore model. I initialize the cells like this:
Gtk.CellRendererText valueCells = new Gtk.CellRendererText ();
valueCells.editable = true;
tree_view.insert_column_with_attributes (-1, "Key", valueCells, "text", 0);
tree_view.insert_column_with_attributes (-1, "Value", valueCells, "text", 1);
I'm now able to select and edit the columns until I exit the Selection. Neither the TreeView nor the ListStore is updated. Tried several solutions I found written in different languages but nothing worked. I understood i have to update the model, but couldn't figure out how to find the reference to that.. Do I miss something essential? O.o
Upvotes: 3
Views: 527
Reputation: 21
using Gtk;
public class viewWindow:Gtk.Window {
public viewWindow() {
this.destroy.connect(Gtk.main_quit);
set_default_size(200,150);
Gtk.ListStore list_store=new Gtk.ListStore(1, typeof(string));
Gtk.TreeIter iter;
list_store.append(out iter);
list_store.set(iter, 0, "Earth");
list_store.append(out iter);
list_store.set(iter, 0, "Mars");
Gtk.TreeView view=new TreeView.with_model(list_store);
this.add(view);
Gtk.CellRendererText cell=new Gtk.CellRendererText();
view.insert_column_with_attributes(-1, "Planet", cell, "text", 0);
cell.editable = true;
cell.edited.connect ((path, data) => {
Gtk.TreePath tPath = new Gtk.TreePath.from_string(path);
var model = view.get_model();
var res = model.get_iter(out iter, tPath);
if (res == true) {
list_store.set(iter, 0, data);
}
});
}
public static int main(string[] args) {
Gtk.init(ref args);
var view = new viewWindow();
view.show_all();
Gtk.main();
return 0;
}
}
Upvotes: 2
Reputation: 14873
You have to connect the edited
signal to get notified of changes made by the user, here is a complete example:
class MainWindow : Gtk.Window {
public MainWindow () {
Gtk.TreeView tree_view = new Gtk.TreeView ();
setup_treeview (tree_view);
add(tree_view);
}
private void setup_treeview (Gtk.TreeView view) {
var listmodel = new Gtk.ListStore (4, typeof (string), typeof (string),
typeof (string), typeof (string));
view.set_model (listmodel);
view.insert_column_with_attributes (-1, "Account Name", new Gtk.CellRendererText (), "text", 0);
view.insert_column_with_attributes (-1, "Type", new Gtk.CellRendererText (), "text", 1);
var cell = new Gtk.CellRendererText ();
cell.set ("foreground_set", true);
cell.editable = true;
cell.edited.connect ((path, new_text) => {
stdout.printf (path + "\n");
stdout.printf (new_text + "\n");
stdout.flush ();
});
view.insert_column_with_attributes (-1, "Balance", cell, "text", 2, "foreground", 3);
Gtk.TreeIter iter;
listmodel.append (out iter);
listmodel.set (iter, 0, "My Visacard", 1, "card", 2, "102,10", 3, "red");
listmodel.append (out iter);
listmodel.set (iter, 0, "My Mastercard", 1, "card", 2, "10,20", 3, "red");
}
}
int main (string[] args) {
Gtk.init (ref args);
MainWindow window = new MainWindow ();
window.title = "Tree View test";
window.border_width = 10;
window.window_position = Gtk.WindowPosition.CENTER;
window.set_default_size (350, 200);
window.destroy.connect (Gtk.main_quit);
window.show_all ();
Gtk.main ();
return 0;
}
The important bit is here:
var cell = new Gtk.CellRendererText ();
cell.editable = true;
cell.edited.connect ((path, new_text) => {
stdout.printf (path + "\n");
stdout.printf (new_text + "\n");
stdout.flush ();
});
It will print the path and the new_text of the modified cell to the command line.
All you have to do now is to update the model accordingly.
Upvotes: 3