How to use Gtk ListStore with Rust


#1

Hi all,
I’m still writing my first GUI with Gtk+3.

I’m stuck trying to insert elements in a ListStore and view them on the TreeView.

I created a GUI with Glade. My ListStore has just one column of gchararray type.
The problem is that when I try to insert strings in it I get only an empty row in the View.

I think this is the important part of the code:

...
fn main() {

    let u = Rc::new(RefCell::new(Universe::new()));

    if gtk::init().is_err(){
        panic!("Fatal error! Unable to initialize Graphic Interface")
    }

    let builder = Builder::new();
    builder.add_from_string(GLADE_SRC).unwrap();
...
    let lights_list: ListStore = builder.get_object("light_list").unwrap();
    /* Add light dialog */
    let tmp_button: ToolButton = builder.get_object("add_light_btn").unwrap();
    tmp_button.connect_clicked(clone!(
        u, stat, window, lights_list => move |_| {
            add_light(&window, stat.clone(), u.clone(), lights_list.clone());
        }
    ));
...
    gtk::main();
}

fn add_light(main_window: &ApplicationWindow, status_bar: Statusbar, universe: Rc<RefCell<Universe>>, list: ListStore){
...
    let name: Entry = builder.get_object("light_name").unwrap();
    let ok_bttn: Button = builder.get_object("add_light_ok").unwrap();
    ok_bttn.connect_clicked(clone!(
        add_dialog, name, builder, list => move |_| {
 ...
            let name = Rc::new(...)
 ...
            ok_button.connect_clicked(clone!(
                add_dialog, status_bar, universe, name, builder, list => move |_| {
...
                    //let iter = list.append();
                    //let v = Value::from(universe.borrow().get_name(l));
                    //println!("{:?}", v);
                    //list.set_value(&iter, 0, &v);
                    let u = universe.borrow();
                    let name = u.get_name(l).unwrap();
                    println!("{}", name);
                    list.insert_with_values(None,
                                            &[0],
                                            &[&name]);
                    add_dialog.destroy();
                }
...
    add_dialog.run();
}

Anyway you can find the full source at http://git.savannah.nongnu.org/cgit/lcs.git/tree/src/main.rs

Can someone help me to understand what am I doing wrong? Thank you!


#2

The problem was that, crating the GUI (i.e. the TreeView and the ListStore) with Glade, it didn’t associate the column with the model’s column, as I expected, so I needed to do this manually getting the TreeViewColumn from the builder and adding a CellRendererText to it.


#3

I found that I could insert the Renderer definition inside the builder source, but, since Glade doesn’t handle well this feature, I had to do this manually.
Then it’s not a problem for Glade to do operations on the source and leave the piece inserted manually where it is, just it seems it’s not able to insert the code.