2

我正在尝试将 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>
4

2 回答 2

2

除非您致电,否则林间空地文件中的信号将保持断开状态gtk_builder_connect_signals()

于 2012-02-01T08:52:04.383 回答
1

如果您打算将程序移植到 Windows,您可能还需要在函数原型/定义中使用 G_MODULE_EXPORT:

G_MODULE_EXPORT void my_callback(void); /* For example */
于 2014-05-30T09:40:30.273 回答