有什么简单的方法可以使Gtk.Treeview在编辑时更新其列?
我基于Gtk.ListStore模型构建Treeview。我像这样初始化单元格:
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);
我现在可以选择和编辑列,直到退出选择为止。 TreeView和ListStore均未更新。我尝试了几种解决方案,发现它们是用不同的语言编写的,但是没有用。我知道我必须更新模型,但无法找出如何找到该模型的参考。我会错过一些重要的东西吗?哦
答案 0 :(得分:3)
您必须连接edited
信号以通知用户所做的更改,这是一个完整的示例:
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;
}
重要的一点在这里:
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 ();
});
它将打印路径和修改后的单元格的new_text到命令行。
您现在要做的就是相应地更新模型。
答案 1 :(得分:1)
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;
}
}