How do I determine the focus order?

Hi, I’m using gtkmm 4 and I would like to set the focus order for the widgets in my ApplicationWindow, how do I do that?

If you just want to set the first child to be focuses, you can use gtk_widget_grab_focus() or set the default widget of the window.

If you want to determine the next widget that the focus should be moved to, then you override the GtkWidgetClass.move_focus in your container and move the focus depending on the direction.

In general, determining the focus order is an indication of something funky in your UI. The focus order is determined by the order in which the children appear to the user, as it’s part of the accessibility layer. Changing the focus order might look all right to you, but for people navigating your UI with a screen reader, for instance, it might lead to some very broken user experience.

Actually I have just one widget in the scenario we’re discussing, I thought I’d take the opportunity to learn about focus order.
I have a few pages in a stack, one of them has a listbox, which I want to st the focus to as soon as the page is shown.
I tried calling grab_focus() and set_receives_default(true) from the page Ctor, but
I still have to press the button in order for the focus to reach the listbox. I want the listbox to respond to arrow keys as soon as the page appears. How can i do that?

Have you tried calling gtk_widget_grab_focus() on the list box in response to the notify::visible-child or notify::visible-child-name signal?

No, I was hoping to find something simpler, like setting the listbox as the default widget.
I have several pages, does this mean I have to register for the stack event, check which is the visible child and call something like OnShow() for that child?

I have no idea what “OnShow()” would be, but yes: the mechanism for doing something when a stack page becomes the visible page is to use the notify signal. Since you know the structure of each page, you can determine which child should have the focus. Stack pages are not widgets: they are ancillary objects that hold a bunch of properties; widgets are children of the stack itself, and they are visible by default, so you cannot use the widget visibility property to see if they are the current page of the stack.

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.