0
fn build_table_view() -> (TreeView, ListStore) {
    let list_store = ListStore::new(&[glib::Type::STRING].repeat(4));
    list_store.n_columns();
    let tree_view = TreeView::builder()
        .hexpand(true)
        .vexpand(true)
        .enable_grid_lines(gtk::TreeViewGridLines::Both)
        .model(&list_store)
        .build();


    for (i, title) in ["Website", "Username", "Password"]
        .iter()
        .enumerate()
    {
        let renderer = CellRendererText::builder()
            .editable(true)
            .xalign(0.0)
            .single_paragraph_mode(true)
            .build();

        tree_view.insert_column_with_attributes(
            i.try_into().unwrap(),
            title,
            &renderer,
            &[(&"text", i.try_into().unwrap())],
        );

        let list_store_copy = list_store.clone();
        renderer.connect_edited(move |_renderer, row, text| {
            list_store_copy.set_value(
                &list_store_copy.iter(&row).unwrap(),
                i.try_into().unwrap(),
                &text.to_value()
            );
        });
    }


    let add_button = gtk::Button::with_label("Add");
    //add_button.connect_clicked(clone!())

    list_store.insert_with_values(
        None,
        &[
            (0, &"Test1")
        ],
    );
    (tree_view, list_store)
}

fn build_gtk4_table_ui_v1(application: &Application) {
    let window = ApplicationWindow::new(application);

    window.set_title(Some("Sammy's Safe"));
    window.set_default_size(800, 300);

    let grid = Grid::builder()
        .column_spacing(5)
        .row_spacing(10)
        .build();

    window.set_child(Some(&grid));

    let (tree_view, _list_store) = build_table_view();

    let list_store_copy = _list_store.clone();

    let sw = ScrolledWindow::builder()
        .child(&tree_view)
        .vexpand(false)
        .vexpand_set(false)
        .max_content_height(10)
        .build();

    // column, row, width, height
    grid.attach(&sw, 0, 0, 1, 1);
    window.show();
} 

I tried following the basic button format that most examples show and just tried including add_button.connect_clicked(move|_, row, text| {list_store.insert_with_values(...)} to no avail. I've been mulling over GTK4 documentation and trying different things with nothing sticking yet. What I want to happen is for the add button to just create a new row with empty strings considering that I got the edit row to already work. A point in the right direction would be great :)

0 Answers0