如何连接 GtkAction 信号?
我正在尝试将 GtkAction 信号连接到回调 open_file 但显然我遗漏了一些东西,因为当我在文件菜单中选择“打开”时没有任何反应。有什么线索吗?
测试.c
#include <gtk/gtk.h>
void open_file(GtkAction *action, gpointer user_data)
{
g_print("open_file\n");
}
int main(int argc, char *argv[])
{
GtkBuilder *builder;
GObject *window;
gtk_init(&argc, &argv);
builder = gtk_builder_new();
gtk_builder_add_from_file(builder, "test.ui", NULL);
window = gtk_builder_get_object(builder, "window");
g_signal_connect(window, "destroy", G_CALLBACK(gtk_main_quit), NULL);
gtk_widget_show_all(GTK_WIDGET(window));
gtk_main();
return 0;
}
测试.ui
<interface>
<object class="GtkUIManager" id="uiman">
<child>
<object class="GtkActionGroup" id="actiongroup">
<child>
<object class="GtkAction" id="file">
<property name="label">_File</property>
</object>
</child>
<child>
<object class="GtkAction" id="open">
<property name="stock_id">gtk-open</property>
<signal name="activate" handler="open_file"/>
</object>
</child>
</object>
</child>
<ui>
<menubar name="menu_bar">
<menu action="file">
<menuitem action="open"/>
</menu>
</menubar>
</ui>
</object>
<object id="window" class="GtkWindow">
<property name="title">Test</property>
<child>
<object class="GtkVBox" id="vbox">
<child>
<object class="GtkMenuBar" id="menu_bar" constructor="uiman"/>
<packing>
<property name="expand">FALSE</property>
</packing>
</child>
</object>
</child>
</object>
</interface>
I'm trying to connect a GtkAction signal to the callback open_file but apparently I am missing something since nothing happens when I select Open in the file menu. Any clues?
test.c
#include <gtk/gtk.h>
void open_file(GtkAction *action, gpointer user_data)
{
g_print("open_file\n");
}
int main(int argc, char *argv[])
{
GtkBuilder *builder;
GObject *window;
gtk_init(&argc, &argv);
builder = gtk_builder_new();
gtk_builder_add_from_file(builder, "test.ui", NULL);
window = gtk_builder_get_object(builder, "window");
g_signal_connect(window, "destroy", G_CALLBACK(gtk_main_quit), NULL);
gtk_widget_show_all(GTK_WIDGET(window));
gtk_main();
return 0;
}
test.ui
<interface>
<object class="GtkUIManager" id="uiman">
<child>
<object class="GtkActionGroup" id="actiongroup">
<child>
<object class="GtkAction" id="file">
<property name="label">_File</property>
</object>
</child>
<child>
<object class="GtkAction" id="open">
<property name="stock_id">gtk-open</property>
<signal name="activate" handler="open_file"/>
</object>
</child>
</object>
</child>
<ui>
<menubar name="menu_bar">
<menu action="file">
<menuitem action="open"/>
</menu>
</menubar>
</ui>
</object>
<object id="window" class="GtkWindow">
<property name="title">Test</property>
<child>
<object class="GtkVBox" id="vbox">
<child>
<object class="GtkMenuBar" id="menu_bar" constructor="uiman"/>
<packing>
<property name="expand">FALSE</property>
</packing>
</child>
</object>
</child>
</object>
</interface>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
除非您调用
gtk_builder_connect_signals()
,否则 Glade 文件中的信号将保持断开状态。The signals in the glade file will remain disconnected unless you call
gtk_builder_connect_signals()
.如果您打算将程序移植到 Windows,您可能还需要在函数原型/定义中使用 G_MODULE_EXPORT:
If you intend to port your program to windows you may also need to use G_MODULE_EXPORT in your function prototypes/definitions: