I have a list model defined as a Gtk.SortListModel on top of a Gio.ListStore. I add an item to my store and would like to know its position in my list_model. The code below illustrates the problem:
# Create a list store
store = Gio.ListStore(type=Item)
store.append(item_1)
...
store.append(item_n)
# Define a sorter
sorter = Gtk.StringSorter.new(...)
# Define my list model
list_model=Gtk.SortListModel.new(store, sorter)
# Next, add an item to the store in a known position, e.g. to the end
# Position of "item_x" in the store is n
store.append(item_x)
# how do I define the position in my list_model to get item_x?
position = ??? # How do I define it here?
list_model.get_item(position) # should return item_x
Gemini suggests the two options that look sub-optimal to me:
- Use a linear search on the list_model to find the newly added item.
- Connect ‘item-changed‘ signal to the list_model and correlate its call with the inserted item.
Is there a better option?