How to support Custom Widget on PopOver Menu?

I am trying to add Custom Widget using below code. But no custom widget is shown..
What calls I am missing here ?

#include <gtk/gtk.h>

static void on_button_clicked (GtkWidget *button, gpointer user_data) {
    // 1) Create the menu model
    GMenu *menu = g_menu_new();
    g_menu_append(menu, "Item 1", NULL);
    g_menu_append(menu, "Item 2", NULL);

    // 2) Create the GtkPopoverMenu
    GtkWidget *popover = gtk_popover_menu_new_from_model(G_MENU_MODEL(menu));

    // 3) Attach popover to the button
    gtk_widget_set_parent(popover, button);

    // 4) Create a custom child (box + label)
    GtkWidget *box = gtk_box_new(GTK_ORIENTATION_HORIZONTAL, 6);
    GtkWidget *label = gtk_label_new("Custom Widget Here");

    gtk_box_append(GTK_BOX(box), label);
    gtk_widget_set_visible(label, TRUE);

    // 5) Add the custom child to the popover
    gtk_popover_menu_add_child(GTK_POPOVER_MENU(popover), box, "custom-1");

    // 6) Show the popover
    gtk_widget_show(popover);
}

static void activate (GtkApplication *app, gpointer user_data) {
    GtkWidget *window = gtk_application_window_new(app);
    gtk_window_set_title(GTK_WINDOW(window), "GTK4 Popover with Custom Child");
    gtk_window_set_default_size(GTK_WINDOW(window), 300, 200);

    GtkWidget *button = gtk_button_new_with_label("Click me");
    g_signal_connect(button, "clicked", G_CALLBACK(on_button_clicked), NULL);

    gtk_window_set_child(GTK_WINDOW(window), button);
    gtk_window_present(GTK_WINDOW(window));
}

int main (int argc, char *argv[]) {
    GtkApplication *app = gtk_application_new("org.example.popover.custom", G_APPLICATION_DEFAULT_FLAGS);

    g_signal_connect(app, "activate", G_CALLBACK(activate), NULL);

    int status = g_application_run(G_APPLICATION(app), argc, argv);

    g_object_unref(app);

    return status;
}

You’re missing a whole custom widget.

You cannot add a GtkPopover to any random widget: you need to create a custom widget class that holds both the popover and the widget that should display it.

If you want to have a button with a popover, use GtkMenuButton.

I have fixed my example.. But now I have issue with displaying icon..

For custom widget icon is shown.. for default menu item it is not shown.

static void on_button_clicked(GtkWidget *button, gpointer user_data) {

GMenu \*menu = g_menu_new();

// Insert custom placeholder at position 0 (top)

GMenuItem \*custom_item1 = g_menu_item_new("Custom Area", NULL);

g_menu_item_set_attribute(custom_item1, "custom", "s", "custom-1");

g_menu_insert_item(menu, 0, custom_item1);

g_object_unref(custom_item1);

// Create menu items with labels

GMenuItem \*item1 = g_menu_item_new("Item 1", NULL);

// Create themed icons

GIcon \*icon1 = g_file_icon_new(g_file_new_for_path("/home/user/Downloads/file.png"));

// Attach icons to the items

g_menu_item_set_icon(item1, icon1);

// Append items into the menu

g_menu_append_item(menu, item1);

// Cleanup

g_object_unref(item1);

g_object_unref(icon1);

// Create popover from model

GtkWidget \*popover = gtk_popover_menu_new_from_model(G_MENU_MODEL(menu));

gtk_widget_set_parent(popover, button);

GtkWidget \*box = gtk_box_new(GTK_ORIENTATION_HORIZONTAL, 6);

GtkWidget \*image = gtk_image_new_from_file("/home/user/Downloads/file.png");

GtkWidget \*label = gtk_label_new("Custom Image");

gtk_box_append(GTK_BOX(box), image);

gtk_box_append(GTK_BOX(box), label);

gtk_widget_set_visible(box, TRUE);

gtk_popover_menu_add_child(GTK_POPOVER_MENU(popover), box, "custom-1");

// Show the popover

gtk_widget_set_visible(popover, TRUE);

}

Background: I was trying to support icon in Eclipse SWT.. currently in SWT for popover menu consumption icon support is missing.. One idea was to support it through custom widget(Box+Image). I was thinking custom widget can be supported on Item.. But it seems custom widget is a new menu item itself..

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