Re: [Vala] Take data from a TreeStore



Hi.

I'm testing the GtkTreeView with GtkTreeStore but don't find in the doc how to get value of an selected row.

You'll need to use GtkTreeSelection for this task. I modified sample
code from Vala's home page to show you how to monitor selection
changes:

---- CODE ----
using Gtk;

public class TreeViewSample : Window {
    private TreeSelection selection;

    public TreeViewSample () {
        this.title = "TreeView Sample";
        set_default_size (250, 100);
        var view = new TreeView ();
        setup_treeview (view);

        /* Monitor selection changes */
        this.selection = view.get_selection();
        this.selection.changed.connect (print_row);

        add (view);
        this.destroy.connect (Gtk.main_quit);
    }

    private void print_row () {
        TreeModel model;
        TreeIter  iter;

        if( this.selection.get_selected (out model, out iter) ) {
            string card;

            model.get (iter, 0, out card);
            stdout.printf ("SELECTED: %s\n", card);
        }
    }

    private void setup_treeview (TreeView view) {

        /*
         * Use ListStore to hold accountname, accounttype, balance and
         * color attribute. For more info on how TreeView works take a
         * look at the GTK+ API.
         */

        var listmodel = new ListStore (4, typeof (string), typeof (string),
                                          typeof (string), typeof (string));
        view.set_model (listmodel);

        view.insert_column_with_attributes (-1, "Account Name", new
CellRendererText (), "text", 0);
        view.insert_column_with_attributes (-1, "Type", new
CellRendererText (), "text", 1);

        var cell = new CellRendererText ();
        cell.set ("foreground_set", true);
        view.insert_column_with_attributes (-1, "Balance", cell,
"text", 2, "foreground", 3);

        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");
    }

    public static int main (string[] args) {
        Gtk.init (ref args);

        var sample = new TreeViewSample ();
        sample.show_all ();
        Gtk.main ();

        return 0;
    }
}
---- CODE ----

Tadej

-- 
Tadej Borovšak
tadeboro.blogspot.com
tadeboro gmail com
tadej borovsak gmail com



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]