I’ve developed an Adw application. I’m going to give quite a bit of context because I don’t know enough about Adw/Gtk to know what will be relevant.
I have a Gtk.DropDown in my app’s toolbar. I create it using Gtk.DropDown.new_from_strings() in my Adw.ApplicationWindow subclass before calling self.present().
I connect the “notify-selected” signal to my handler for dealing with changing the selected item in the dropdown. I don’t do anything in the app until someone chooses from this dropdown to decide what to work on. I do not want a default.
What I’m seeing is that the first item in the dropdown is always pre-selected. Clicking on it doesn’t send a signal (obviously because the selection isn’t changing.) This means that if I want to select the first item, I have to select another and then come back and select the first item.
I’ve tried doing my_dropdown.set_selected(Gtk.INVALID_LIST_POSITION) but this makes no difference. I’ve tried making that call before connecting the signal, after connecting the signal, and from a GLib.idle_add but there’s no visible change.
As a workaround I’ve resorted to creating a dummy entry at the start of the list of strings I use to populate the DropDown, and then remove the first entry from my_dropdown.get_model() and reselect the appropriate entry to reflect the new model contents.
Is there anything better I can do?
Code if needed:
names.insert(0, self.DUMMY_COLLECTION_NAME)
self.collection_box: Gtk.DropDown = Gtk.DropDown.new_from_strings(names)
# Make the collection box's button in the toolbar look flat to match the rest of the buttons
self.collection_box.add_css_class('flat-dropdown')
self.collection_box.connect('notify::selected', self.collection_chosen)
Thanks @Holger. Where is that call in relation to creating and populating the dropdown? I’ve tried selecting the INVALID_LIST_POSITION in various places and the first list item is always selected regardless.
Thanks. The main difference I see is that your code creates a model and sets it. My code uses Gtk.DropDown.new_from_strings to create an implicit Gtk.StringList. I’ll make a small Python app equivalent to your sample and play with that.
I’ve tested a small Python app equivalent to @Holger C code. It shows the same problem - the first item in the dropdown is pre-selected as soon as the app starts. Thanks for the help - I’m going to stick with pre-pending a dummy entry to my list of valid values.