How do I initialize a GActionMap variable?

﹥>﹥吖頭↗ 提交于 2021-02-05 07:35:22

问题


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.


回答1:


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));


来源:https://stackoverflow.com/questions/57261076/how-do-i-initialize-a-gactionmap-variable

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!