cgtkgobjectgio

How do I initialize a GActionMap variable?


I want to know how to initialize a GActionMap variable.

I search here https://developer.gnome.org/gio/stable/GActionMap.html to find a function that instances a GActionMap, but I didn't find it there and anywhere.

I was trying to insert some actions in my application, to use the function item = g_menu_item_new(labelItem, "sair"); where item is a menu item, labelItem is a menu item label and sair is the function name that I want to trigger when the item is clicked.

But when I run the code, I get "segmentation fault", that stop the execution specifically in g_action_map_add_action(grupo, G_ACTION(acao));:

    GSimpleAction *acao;
    GActionMap *grupo;

    acao = g_simple_action_new("sair", NULL);
    g_signal_connect(
        G_OBJECT(acao), "activate", G_CALLBACK(sair), window);

    g_action_map_add_action(grupo, G_ACTION(acao));
    gtk_widget_insert_action_group(
        window, "grupo", G_ACTION_GROUP(grupo));

window is the application window.

The message error is the following:

GLib-GObject-CRITICAL **: 17:01:01.711: g_type_interface_peek: assertion 'instance_class != NULL' failed
[1]    11396 segmentation fault (core dumped)

I'm using gtk4.


Solution

  • According to the documentation, GActionMap is an interface (https://developer.gnome.org/gio/stable/GActionMap.html) which is implemented, for example, by GApplication (https://developer.gnome.org/gio/stable/GApplication.html).

    Therefore you should pass a GApplication instance (or another type that implements the interface GActionMap) to the function like it is done in this tutorial: https://developer.gnome.org/GAction/ for a similar usecase (different functions, same inputs).

    Should the link broke I replicate here the code that you should write, assuming that you have access to a correctly initialized instance of a GApplication:

    GApplication * app = ...
    ...some other code ...
    g_action_map_add_action(G_ACTION_MAP(app), G_ACTION(acao));