gpt4 book ai didi

treeview - Vala 中的 Gtk.TreeView 可编辑列

转载 作者:行者123 更新时间:2023-12-04 07:47:27 26 4
gpt4 key购买 nike

有没有什么简单的方法可以让 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 都没有更新。尝试了几种我发现用不同语言编写的解决方案,但没有任何效果。我知道我必须更新模型,但无法弄清楚如何找到对此的引用。我是否错过了一些重要的东西?哦

最佳答案

您必须连接 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打印到命令行。

您现在要做的就是相应地更新模型。

关于treeview - Vala 中的 Gtk.TreeView 可编辑列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53181128/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com