Skip to content

Instantly share code, notes, and snippets.

@micaiah-effiong
Created August 13, 2024 13:48
Show Gist options
  • Select an option

  • Save micaiah-effiong/a2a17a0636d4f599eb2fbc62482de678 to your computer and use it in GitHub Desktop.

Select an option

Save micaiah-effiong/a2a17a0636d4f599eb2fbc62482de678 to your computer and use it in GitHub Desktop.
Get the selected element/list-item from gtk4 listview

Problem:

Selecting an item from GTK ListView widget.
Related issue https://discourse.gnome.org/t/gtk4-listview-why-does-the-single-click-activate-also-trigger-the-selected-event/7947

Solution:

// let list_view: gtk::ListView;
if let Some(model) = list_view.model() {
    model.connect_selection_changed(move |selection_model, _, _| { // listen for selection change
        let single_selection_model =
            match selection_model.downcast_ref::<gtk::SingleSelection>() { // down cast to your model of choice(eg: single selection model)
                Some(ss) => ss,
                None => return,
            };

        let list_item = single_selection_model.selected_item();
        let pos = single_selection_model.selected();
    });
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment