How to position labels over a texture

Hi, I’m using Gtkmm4, and I need to position some labels over a texture, as seen in the following image:

I was wondering what is the best way to do that, I’m thinking of adding a grid on top of the texture, and attaching the labels to it.
How can I add a grid on top of a Gtk::Picture class? Should I use Gtk::Overlay to hold the Grid?
Other suggestions are welcome too

A grid is not meant for absolute or relative positioning. You will probably have an easier time implementing a custom widget and laying out the positions of the text yourself.

If the labels do not need to be interactive, you can just use Gtk::Snapshot::render_layout in the snapshot vfunc. If they do need to be interactive, you can create some Gtk::Labels as children and then position them in your size_allocate vfunc.

Thx for the quick reply, but I’m afraid I don’t understand how to use Gtk::Snapshot::render_layout.
Can you give an example or psuedo code?

The simplest I could think of would be a sample usage in the gtk4 source code, simplified you can do something like:

static void
my_widget_snapshot (GtkWidget *widget, GtkSnapshot *snapshot)
  double x, y; // TODO - set position
  PangoLayout *layout = gtk_widget_create_pango_layout (widget, "my text");
  gtk_snapshot_render_layout (snapshot,
                              gtk_widget_get_style_context (GTK_WIDGET (area)),
                              x, y,
  g_object_unref (layout);

It should be very similar in C++. See the custom widget example for how to create a subclass with a snapshot vfunc.

THX! I’ll give it a go :slight_smile:

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