如何连接Gtk.TreeView的信号,以便在选择行时收到通知?目前我正在连接row_activated信号,但这需要双击该行,我希望只需点击一下就会收到通知。
示例程序:
using Gtk;
public class MyListView : ScrolledWindow {
ListStore list_store;
TreeView tree_view;
GLib.List<string> list;
enum Columns {
TEXT,
N_COLUMNS
}
void make_list () {
list = new GLib.List<string> ();
list.append("Hello World");
list.append("row 2");
list.append("<b>bold</b>");
list.append("<i>italic</i>");
list.append("...");
list.append("etc.");
}
public MyListView () {
make_list();
list_store = new ListStore(Columns.N_COLUMNS, typeof(string));
tree_view = new TreeView.with_model(list_store);
var text = new CellRendererText ();
var column = new TreeViewColumn ();
column.pack_start (text, true);
column.add_attribute (text, "markup", Columns.TEXT);
tree_view.append_column (column);
tree_view.set_headers_visible (false);
TreeIter iter;
foreach (string item in list) {
list_store.append(out iter);
list_store.set(
iter,
Columns.TEXT, item
);
}
this.add(tree_view);
tree_view.row_activated.connect(change);
}
public void change (TreePath path, TreeViewColumn col) {
var index = int.parse(path.to_string());
var item = list.nth_data(index);
print(index.to_string() + ". " + item + "\n");
}
}
public static void main (string[] args) {
Gtk.init(ref args);
var win = new Window();
win.add(new MyListView());
win.show_all();
win.destroy.connect(Gtk.main_quit);
Gtk.main();
}
除了row_activated的双击要求外,此程序完全按照我的要求工作。
答案 0 :(得分:6)
TreeSelection的已更改信号(使用tree.get_selection()
获取),与cursor_changed相比应该是正确的方式。
答案 1 :(得分:0)
要通过单击获得通知,请连接到cursor_changed信号。这不会将任何变量暴露给回调函数,因此您需要定义一个单独的函数来获取所选项的索引:
此函数可以使用TreeView对象的get_selection()方法来获取TreeSelection对象。使用single selection方法将选择模式设置为set_mode。然后,您可以使用TreeModel方法的out参数获取TreeIter和get_selected。您应该检查此函数的返回值 - 如果选择了某些内容,则为true;如果未选择任何内容,则为false。然后,使用TreeModel的get_path方法获取所选项目的TreePath。
完整示例:
using Gtk;
public class MyListView : ScrolledWindow {
ListStore list_store;
TreeView tree_view;
GLib.List<string> list;
enum Columns {
TEXT,
N_COLUMNS
}
void make_list () {
list = new GLib.List<string> ();
list.append("Hello World");
list.append("row 2");
list.append("<b>bold</b>");
list.append("<i>italic</i>");
list.append("...");
list.append("etc.");
}
public MyListView () {
make_list();
list_store = new ListStore(Columns.N_COLUMNS, typeof(string));
tree_view = new TreeView.with_model(list_store);
var text = new CellRendererText ();
var column = new TreeViewColumn ();
column.pack_start (text, true);
column.add_attribute (text, "markup", Columns.TEXT);
tree_view.append_column (column);
tree_view.set_headers_visible (false);
TreeIter iter;
foreach (string item in list) {
list_store.append(out iter);
list_store.set(
iter,
Columns.TEXT, item
);
}
this.add(tree_view);
tree_view.cursor_changed.connect(change);
}
public int get_selected () {
var selection = tree_view.get_selection();
selection.set_mode(SelectionMode.SINGLE);
TreeModel model;
TreeIter iter;
if (!selection.get_selected(out model, out iter)) {
return -1;
}
TreePath path = model.get_path(iter);
return int.parse(path.to_string());
}
public void change () {
var index = this.get_selected();
if (index >= 0) {
var item = list.nth_data(index);
print(index.to_string() + ". " + item + "\n");
}
}
}
public static void main (string[] args) {
Gtk.init(ref args);
var win = new Window();
win.add(new MyListView());
win.show_all();
win.destroy.connect(Gtk.main_quit);
Gtk.main();
}