cgtkgobject

How do I connect GtkAction signal?


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>

Solution

  • The signals in the glade file will remain disconnected unless you call gtk_builder_connect_signals().