选择树视图(列表存储)行时如何收到通知?
如何连接到 Gtk.TreeView< 的信号/a> 以便在选择一行时通知我?目前我正在连接到 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 的双击要求之外,该程序的工作原理与我想要的完全一样。
How can I connect to a signal of a Gtk.TreeView so that I am notified when a row is selected? Currently I am connecting to the row_activated signal but this requires a double click on the row and I want to be notified on a single click.
Example program:
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();
}
This program works exactly as I want except for the double click requirement of row_activated.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这里有 TreeSelection 的 changed 信号(使用
tree.get_selection()
获取它),与cursor_changed 相比,这应该是正确的方法。There's the changed signal of TreeSelection (get it using
tree.get_selection()
), which should be the right way compared to cursor_changed.要通过单击获得通知,请连接到 cursor_changed 信号。这不会向回调函数公开任何变量,因此您需要定义一个单独的函数来获取所选项目的索引:
此函数可以使用 get_selection() TreeView 对象的方法来获取 TreeSelection 对象。将选择模式设置为单选使用 set_mode 方法。然后您可以获得 TreeModel 和 TreeIter 使用get_selected 方法的输出参数。您应该检查此函数的返回值 - 如果选择了某些内容,则返回 true;如果未选择任何内容,则返回 false。然后,使用 get_path 方法TreeModel 获取 所选项目的TreePath。
完整示例:
To get notified on a single click, connect to the cursor_changed signal. This doesn't expose any variables to the callback function so you need to define a separate function that gets the index of the selected item:
This function can use the get_selection() method of the TreeView object to get a TreeSelection object. Set the selection mode to single selection using the set_mode method. You can then get the TreeModel and TreeIter using the out parameters of the get_selected method. You should check the return value of this function - true if something is selected and false if nothing is selected. Then, use the get_path method of the TreeModel to get the TreePath of the selected item.
Full Example: