[GTK4] GtkPopover Finalizing warning

I wanted to try GTKPopover, which would work except for the warning at the end.
Anyone have any idea what I’m doing wrong?

// gcc -o main main.c `pkg-config --cflags --libs gtk4`

#include <gtk/gtk.h>

static void btn_item_click_cp(GtkWidget *widget, gpointer user_data) {
 g_print("Es wurde geklick: %s\n", (char*) user_data);
}

static void on_button_clicked(GtkWidget *widget, gpointer user_data) {
    gtk_popover_popup(user_data);
}

static void activate(GtkApplication *app, gpointer user_data) {
    GtkWidget *window = gtk_application_window_new(app);
    gtk_window_set_title(GTK_WINDOW(window), "GTK4 Popup Menu Example");
    gtk_window_set_default_size(GTK_WINDOW(window), 300, 200);

    GtkWidget *vbox = gtk_box_new(GTK_ORIENTATION_VERTICAL, 10);
    gtk_window_set_child(GTK_WINDOW(window), vbox);

    GtkWidget *button = gtk_button_new_with_label("Popover Button");
    gtk_box_append(GTK_BOX(vbox), button);

    GtkWidget *popover = gtk_popover_new();
    gtk_widget_set_parent(popover, button);

    GtkWidget *popover_box = gtk_box_new(GTK_ORIENTATION_VERTICAL, 5);
    gtk_popover_set_child(GTK_POPOVER(popover), popover_box);

    GtkWidget *item1 = gtk_button_new_with_label("Option 1");
    g_signal_connect(item1, "clicked", G_CALLBACK(btn_item_click_cp), "Option 1");
    gtk_box_append(GTK_BOX(popover_box), item1);

    GtkWidget *item2 = gtk_button_new_with_label("Option 2");
    g_signal_connect(item2, "clicked", G_CALLBACK(btn_item_click_cp), "Option 2");
    gtk_box_append(GTK_BOX(popover_box), item2);

    g_signal_connect(button, "clicked", G_CALLBACK(on_button_clicked), popover);
    gtk_window_present(GTK_WINDOW(window));
}

int main(int argc, char **argv) {
    GtkApplication *app = gtk_application_new("org.gtk.example", 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;
}
$ ./main 

(main:4093): Gtk-WARNING **: 09:08:45.272: Finalizing GtkButton 0x6151d35161e0, but it still has children left:
   - GtkPopover 0x6151d35efb60

This question has already been answered here.

An example of how it can work here:

Have fun programming.

1 Like

The short answer is: you cannot add a GtkPopover to random widgets and expect it to work.

If you want to add a GtkPopover to a GtkButton you will need to:

  1. create a new class that inherits from GtkButton
  2. create a new GtkPopover instance inside the instance initialisation function of your derived button class, and keep a reference to it: button->popover = gtk_popover_new()
  3. call gtk_widget_set_parent(button->popover, button) inside the instance initialisation function of your derived button class
  4. override GObjectClass.dispose and call gtk_widget_unparent(button->popover) inside the dispose implementation

For more information on how to derive GObject types like GtkWidget, you should read the GObject tutorial.

I solved it with a GtkMenuButton as suggested by @Holger.
GtkPopOver seems to be quite complex, as you have to inherit ready-made widgets.
Or have I overlooked something?

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