[Vala] ListStore and SQLite



Hi,
I made a simple program to fill a liststore with a sqlite db (name, age, sex). But, i have a problem, someone knows how to save in the db when user change a cell ?

Thanks in adavance,
Nicolas.

This is my code:

using Gtk;
using Sqlite;

   public TreeView treeview;
   public ListStore liststore;

   public TreeViewColumn column1;
   public TreeViewColumn column2;
   public TreeViewColumn column3;

 private enum Col
 {
   NAME,
   AGE,
   SEX,
   NB
 }


 private void on_column1_edit (string path, string new_text)
 {
   Gtk.TreeIter iter;
   liststore.get_iter_from_string (out iter, path);
   liststore.set (iter, 0, new_text);
 }

 private void on_column2_edit (string path, string new_text)
 {
   Gtk.TreeIter iter;
   liststore.get_iter_from_string (out iter, path);
   liststore.set (iter, 1, new_text);
 }

 private void on_column3_edit (string path, string new_text)
 {
   Gtk.TreeIter iter;
   liststore.get_iter_from_string (out iter, path);
   liststore.set (iter, 2, new_text);
 }


protected void row_select (Gtk.TreeView sender, Gtk.TreePath path, Gtk.TreeViewColumn column)
   {
       Gtk.TreeIter iterDead;
Gtk.TreeModel? treeModel = sender.get_model();
       treeModel.get_iter (out iterDead, path);
liststore.remove (iterDead);
   }

private static int populate_column (int n_columns, string[] values, string[] column_names)
   {
       TreeIter ti;
       liststore.append (out ti);
liststore.set(ti, Col.NAME, values[0], Col.AGE, values[1], Col.SEX, values[2], -1);
       return 0;
   }


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

       Database db;

       int loaddb;
       loaddb = Database.open ("/home/niko/Desktop/test.db", out db);

liststore = new Gtk.ListStore(Col.NB, typeof(string), typeof(string), typeof(string)); treeview = new Gtk.TreeView.with_model(liststore);


   var renderer = new CellRendererText();
   renderer.set_property("editable", true);
   renderer.edited.connect (on_column1_edit);
   renderer.set ("width-chars", 25, null);
   renderer.set ("height", 15, null);
   column1 = new Gtk.TreeViewColumn ();
   column1.pack_start (renderer, true);
   column1.set_title ("Nom");
   column1.add_attribute (renderer, "text", Col.NAME);
   column1.sizing = Gtk.TreeViewColumnSizing.GROW_ONLY;

   var renderer2 = new CellRendererText();
   renderer2.set_property("editable", true);
   renderer2.edited.connect (on_column2_edit);
   column2 = new Gtk.TreeViewColumn ();
   column2.pack_start (renderer2, true);
   column2.set_title ("Age");
   column2.add_attribute (renderer2, "text", Col.AGE);

   var renderer3 = new CellRendererText();
   renderer3.set_property("editable", true);
   renderer3.edited.connect (on_column3_edit);
   column3 = new Gtk.TreeViewColumn ();
   column3.pack_start (renderer3, true);
   column3.set_title ("Sexe");
   column3.add_attribute (renderer3, "text", Col.SEX);

   treeview.append_column (column1);
   treeview.append_column (column2);
   treeview.append_column (column3);

   treeview.set_search_column(0);
   treeview.set_reorderable(true);
   treeview.set_headers_clickable(true);
   treeview.set_rubber_banding(true);
   treeview.set_rules_hint(true);

   int rc;
   rc = db.exec ("select * from personnes", populate_column, null);

   column1.add_attribute (renderer, "text", 0);
   column2.add_attribute (renderer2, "text", 1);
   column3.add_attribute (renderer3, "text", 2);

   treeview.row_activated += row_select;

   var win = new Gtk.Window(Gtk.WindowType.TOPLEVEL);
win.add(treeview); win.show_all(); win.destroy.connect (Gtk.main_quit);
   Gtk.main();
return 0;
 }
}





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