0

How I can update child widget of gtk::Grid (gtk::Label in example) in runtime?

In example code after change value SpinButton, I add recreated Grid (fn grid()) with updated childs (I don't want remove old Grid in example).

In a real project I need to add a Grid with an updateable child element Label without recreate Grid. New values will be continuously read from the database.

Example:

pub fn test(parent: &gtk::Box) {
    let parent_grid = gtk::Grid::new();
    let parent_spin = gtk::SpinButton::with_range(0.0, 10.0, 1.0);
    parent_grid.add(&parent_spin);

    let parent_value = Rc::new(RefCell::new(0));
    let parent_value_clone = parent_value.clone();
    let parent_grid_rc = Rc::new(RefCell::new(parent_grid));
    let parent_grid_rc_clone = parent_grid_rc.clone();

    parent_spin.connect_value_changed(move |x| {
        *parent_value_clone.borrow_mut() = x.value_as_int();
        (*parent_grid_rc_clone.borrow_mut()).add(&grid(*parent_value.borrow()));
    });

    (*parent_grid_rc.borrow()).show_all();
    parent.add(&(*parent_grid_rc.borrow()));
}

fn grid(value: i32) -> gtk::Grid {
    let grid = gtk::Grid::new();
    let label_box = gtk::Label::new(Some("Value: "));
    let value_box = Rc::new(RefCell::new(gtk::Label::new(Some(&format!("{}", value)))));  // THIS LABEL MUST BE UPDATED DURING RUNTIME
    grid.add(&label_box);
    grid.add(&(*value_box.borrow()));
    grid.show_all();
    grid
}

enter image description here

I'm new to this, so if there are other methods for creating dynamic objects in Rust-GTK and modifying their children, I'd love to hear about them.

1 Answer 1

1

Library glib helped me in my trouble. I didn't find any clear examples on the internet, so let my solution be here.

pub fn test(parent: &gtk::Box) {
    let parent_grid = gtk::Grid::new();
    let parent_spin = gtk::SpinButton::with_range(0.0, 10.0, 1.0);
    parent_grid.add(&parent_spin);

    let parent_value = Rc::new(RefCell::new(0));
    let parent_value_clone = parent_value.clone();

    parent_spin.connect_value_changed(move |x| {
        *parent_value_clone.borrow_mut() = x.value_as_int();
    });

    let grid = gtk::Grid::new();
    let label_box = gtk::Label::new(Some("Value: "));
    let value_box = gtk::Label::new(Some("Value"));
    grid.add(&label_box);
    grid.add(&value_box);
    grid.show_all();

    let update_label = move || {
        println!("value={}", *parent_value.borrow());
        value_box.set_text(&format!("{}",*parent_value.borrow()));
        glib::Continue(true)
    };
    glib::timeout_add_seconds_local(2, update_label);

    parent_grid.add(&grid);
    parent_grid.show_all();
    parent.add(&parent_grid);
}



fn grid(value: Rc<RefCell<i32>>) -> (gtk::Grid) {
    let grid = gtk::Grid::new();
    let label_box = gtk::Label::new(Some("Value: "));
    let value_box = gtk::Label::new(Some("Value"));
    grid.add(&label_box);
    grid.add(&value_box);
    let value = *value.borrow();

    grid.show_all();
    let update_label = move || {
        value_box.set_text(&format!("{}",value));
        glib::Continue(true)
    };
glib::timeout_add_seconds_local(1, update_label);
    grid
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.