Release focus from GtkSearchEntry

There’s grab_focus(), but how do I “release” the focus? I’m trying to de-focus the GtkSearchEntry after the user has pressed Enter.

You always have to give focus to something else.

I’ve tried to grab_focus() on GtkBox, but as expected it didn’t work and GtkSearchEntry kept its focus.

I don’t have any other entry (or widget that grab_focus() would work on) that needs focus in my application. Is there really no other way?

Also, out of curiousity, if focus must always be held by something, what happens when I press Tab and the entry defocuses?

I was setting the GtkSearchEntry’s text through code and had the cursor move at the start, so defocusing the entry was my first thought.

Just found out that I can set_position(-1), and that fixes my initial issue. I no longer need to release the focus, so I’ll mark the first answer as the solution :+1:

In gtk4 you can use gtk_root_set_focus(NULL) , after which the corresponding window won’t have a focused widget anymore.

(the “root” widget can be obtain by Gtk.Widget.get_root called on any widget attached to the window)

No idea in gtk3…

That worked, thanks!

query.root().unwrap().set_focus(None as Option<&gtk::Widget>);
1 Like