Get ListRow length in Gtk with Rust
For some reason, gtk::ListBox
doesn't expose a method to get the length of the list.
So... we have to hack around.
tl;dr I don't know why, but none of this methods works. Go for binding a model instead.
It will never be an efficient method, but I firstly thought about iterating over every element. This seems possible in Vala:
// ...
var button = new Button.with_label ("Get count!");
button.clicked.connect (() => {
int count = 0;
listbox.@foreach (() => {
count++;
});
Rust bindings doesn't have a similar `foreach`, so I thought about selecting all the elements, then run `selected_foreach`, then deselecting, like this:
let mut c = 0;
widgets.nucs_listbox.select_all();
widgets.nucs_listbox.selected_foreach(|_, _| {
c+=1;
});
But, I don't know why, it doesn't work. The selection fails without any warning.
So, what about getting the index of the last element? It would be drastically more efficient (should be, I don't know how the library get the last element in the first place):
let c = widgets.nucs_listbox.row_at_y(-1);
let c = c.unwrap().get_index();
Does it works? Nope, because get_index
method is not available in Rust bindings, even if it appears on the official GTK docs.
So, my suggestion is get the long path and binding the ListBox
to a model, no matter how simple it is.
It could be overkill, but it's the most solid way to operate on a ListBox.
Did you find this post useful?
Remember that this website doesn’t make use of any trackers or analytics or adv so it doesn’t earn from your visits (moreover, it has a minimal environmental impact).
If you like this blog, refill my caffeine supplies by