GTK4 : menus and pango markup

Hello world,
few months ago already tried to ask about this here:

If it is possible I am sorry but I did not find a way to re-open / refresh the same topic.
Migrating my app from GTK3 to GTK4 I re-created a bunch of pop menus, menus that need to be able to use pango markup, so basically this is what I was doing with, and what I am doing now:

  1. GTK3
   GtkWidget * markup_menu_item (gchar * label)
     GtkWidget * item = gtk_menu_item_new ();
     GtkWidget * lab = gtk_label_new (label);
     gtk_label_set_use_markup (GTK_LABEL(lab), TRUE);
     gtk_container_add (GTK_CONTAINER(item), lab);
     return item;
  1. GTK4
   GMenuItem * menu_item (const gchar * label, 
                          const gchar * action, 
                          const gchar * accel,
                          const gchar * custom,
                          gboolean use_markup)
     GMenuItem * item = g_menu_item_new (label, action);
     if (accel) g_menu_item_set_attribute (item, "accel", "s", accel, NULL);
     if (custom) g_menu_item_set_attribute (item, "custom", "s", custom, NULL);
     if (use_markup)
       // What would be the attribute (if any ?)
       // g_menu_item_set_attribute (item,  ???, use_markup, NULL);

Here is some illustrations of why I need to be able to do this (chemical formula mostly), first what I have with GTK3:

Then what I have with GTK4:

So my question is, is there anyway to achieve the GTK3 result with GTK4 ?
If not yet, will there be a way ?

Maybe an action that can be performed on the entire menu ?

Thanks for your lights.
All the best.


You have two options:

  1. when defining a menu using XML, you can use the use-markup boolean attribute. Remember to escape the markup inside your XML, so foo<sup>bar</sup> becomes foo&lt;sup&gt;bar&lt;/sup&gt;
  2. when defining a menu programmatically using GMenu, set the use-markup boolean attribute, e.g.
g_menu_item_set_attibute (item, "use-markup", "b", use_markup, NULL);

You will need GTK 4.6 in order to do this, as the “use-markup” attribute was added in that release.


Thank you very much for your answer !

1 Like

Hello again,
I installed GTK 4.6, re-build my code using this suitable version, and tried your solution, the code one since I am not using XML file:

g_menu_item_set_attibute (item, "use-markup", "b", use_markup, NULL);

And I am afraid to tell you that it does not work … not sure what I am doing wrong here …
I am now using Fedora 35 and Gnome 41.3 and X11 can it be related to any it ?

Update: same (bad) result with Wayland.

Fedora 35 does not come with GTK 4.6: it still ships with GTK 4.4.

If you want to try GTK 4.6, I strongly recommend using the GNOME nightly SDK with GNOME Builder.

Yes, Fedora 35 ships with GTK 4.4, I noticed, that is why after installing Fedora 35,
I did build/compiled and install GTK 4.6, yet I got the error and reply to let you know …
Thanks you for letting me know about GNOME nighlty SDK and GNOME builder, I will look into these.

If you built GTK 4.6 and installed it over the system installation, you may have borked your system; if you installed it into a separate prefix, you should ensure that you’re linking against the newly built version of GTK.

I would strongly recommend you use Flatpak and Builder: they should simplify the cases, and avoid breaking your base OS.

I am sorry I am not really familiar with all these tools you are mentioning, but I will look into these as well.
However I am familiar enough with library building and linking to be sure that GTK4.6 was installed in a separate directory (/usr/local) and that I built and linked my program with GTK 4.6,
and also that I am using it at runtime, and that is why I updated my post in the first place.
I do that setting up LD_LIBRARY_PATH in my .bashrc file.
I would not have bothered you unless I was sure to use the appropriate version of GTK … maybe this comes from Gnome Shell using the other version (no idea how to check that, or enforce a version) … anyway I think will have to be patient to see this through.
Thanks for your help.

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