Menus: escape mnemonic

Hello,

When creating a GtkPopoverMenu from a menu model, Gtk4 will always try to parse mnemonics from the menu label (see here)

My problem is that one of my menus contains a list of filenames, so files with underscores in their names will be incorrectly displayed (__init__.py becomes _init_.py, aaa_bbb becomes aaabbb, …)

Is there a way to avoid this, or to escape the mnemonics underscore when creating my GMenuItem?

Note: back in Gtk3 we had 2 different APIs gtk_menu_item_new_with_label() and gtk_menu_item_new_with_mnemonic(), I couldn’t find any equivalent in Gtk4.

No, there’s no such thing as a way to set the label without mnemonic.

I assume you’re building a menu using Gio.Menu and assigning the label; you could replace the menu items with a custom placeholder and then using custom widgets.

1 Like

Yes, I use a Gio.Menu.

I’ll try the custom widget, good idea, thanks!

Well, now with a custom widget (GtkLabel) the text display works (no mnemonic troubles), but I’ve lost all the theming and click/activation events on the menu item, looks like I’ve to implement them myself on my label…

New strategy: I replaced all underscores by the unicode sequence U+2002 U+0332.

It visually looks like an underscore, but doesn’t get detected as mnemonic.
That way I can use Gio.Menu.append() like before, without custom widgets.

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