选择树视图(列表存储)行时如何收到通知?

发布于 2024-12-01 06:05:15 字数 2001 浏览 1 评论 0原文

如何连接到 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

风追烟花雨 2024-12-08 06:05:15

这里有 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.

此刻的回忆 2024-12-08 06:05:15

要通过单击获得通知,请连接到 cursor_changed 信号。这不会向回调函数公开任何变量,因此您需要定义一个单独的函数来获取所选项目的索引:

此函数可以使用 get_selection() TreeView 对象的方法来获取 TreeSelection 对象。将选择模式设置为单选使用 set_mode 方法。然后您可以获得 TreeModelTreeIter 使用get_selected 方法的输出参数。您应该检查此函数的返回值 - 如果选择了某些内容,则返回 true;如果未选择任何内容,则返回 false。然后,使用 get_path 方法TreeModel 获取 所选项目的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();

}

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:

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();

}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文