问题描述
我想知道如何初始化GActionMap变量.
I want to know how to initialize a GActionMap variable.
我在这里搜索 https://developer.gnome.org/gio/stable/GActionMap.html
来找到一个实例化GActionMap的函数,但是我没有在任何地方找到它.
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.
我试图在我的应用程序中插入一些动作,以使用该功能 item = g_menu_item_new(labelItem,"sair");
其中 item
是菜单项,labelItem是菜单项标签,而 sair
是单击该项目时要触发的函数名.
I was trying to insert some actions in my application, to use the functionitem = 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.
但是,当我运行代码时,出现分段错误",这专门在 g_action_map_add_action(grupo,G_ACTION(acao));
:
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));
窗口
是应用程序窗口.
消息错误如下:
GLib-GObject-CRITICAL **: 17:01:01.711: g_type_interface_peek: assertion 'instance_class != NULL' failed
[1] 11396 segmentation fault (core dumped)
我正在使用gtk4.
推荐答案
根据文档, GActionMap
是一个接口( https://developer.gnome.org/gio/stable/GActionMap.html ),例如,它是由 GApplication 代码>( https://developer.gnome.org/gio/stable/GApplication.html).
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).
因此,您应该将 GApplication
实例(或另一种实现接口 GActionMap
的类型)传递给本教程中完成的功能: https://developer.gnome.org/GAction/用于类似的用例(不同的功能,相同的输入).
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).
如果链接断开,我在这里复制您应该编写的代码,假设您可以访问 GApplication
的正确初始化的实例:
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));
这篇关于如何初始化GActionMap变量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!