2012-02-01 70 views
2

我想連接一個GtkAction信號到回調open_file,但顯然我缺少一些東西,因爲當我在文件菜單中選擇打開時沒有任何反應。任何線索?如何連接GtkAction信號?

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> 
+0

是否有在控制檯上的任何錯誤消息? – sarnold 2012-02-01 01:42:11

+0

沒有錯誤消息,也沒有「open_file」消息。 – 2012-02-01 01:44:26

回答

2

,除非你調用gtk_builder_connect_signals()在林間空地文件的信號將保持斷開狀態。

+0

好的,我添加了一行gtk_builder_connect_signals(builder,NULL),但現在我收到錯誤消息「(test:2075):Gtk-WARNING **:找不到信號處理程序'open_file'」。 – 2012-02-01 11:10:30

+0

你在Windows上嗎?然後你必須在'open_file'函數定義之前添加G_MODULE_EXPORT。 – ptomato 2012-02-01 11:19:11

+0

將gmodule-2.0添加到編譯命令中的pkg-config命令後,它可以工作。我還將gtk_builder_connect_signals(builder,NULL)更改爲gtk_builder_connect_signals(builder,window)以獲取處理程序中的窗口(不知道這是否是標準方法)。 – 2012-02-01 11:38:11

1

如果您打算端口你的程序,你可能還需要在你的函數原型/定義使用G_MODULE_EXPORT窗口:

G_MODULE_EXPORT void my_callback(void); /* For example */ 
+0

謝謝,在我使用'--export-all-symbols'之前幫助綁定了我的信號 – Sebastian 2015-01-08 01:42:42