【问题标题】:How do I connect GtkAction signal?如何连接 GtkAction 信号?
【发布时间】:2012-02-23 18:22:07
【问题描述】:

我正在尝试将 GtkAction 信号连接到回调 open_file 但显然我遗漏了一些东西,因为当我在文件菜单中选择打开时没有任何反应。有什么线索吗?

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>

【问题讨论】:

  • 控制台有错误信息吗?
  • 没有错误消息,也没有“open_file”消息。

标签: c gtk gobject


【解决方案1】:

glade 文件中的信号将保持断开状态,除非您致电 gtk_builder_connect_signals()

【讨论】:

  • 好的,我添加了行 gtk_builder_connect_signals(builder, NULL) 但现在我收到错误消息“(test:2075):Gtk-WARNING **:找不到信号处理程序'open_file'”。
  • 您在 Windows 上吗?然后你必须在open_file函数定义前面添加G_MODULE_EXPORT。
  • 在编译命令中将 gmodule-2.0 添加到 pkg-config 命令后,它就可以工作了。我还将 gtk_builder_connect_signals(builder, NULL) 更改为 gtk_builder_connect_signals(builder, window) 以获取处理程序中的窗口(虽然不知道这是否是标准方法)。
  • 是的,gtk_builder_connect_signals() 的第二个参数成为所有以这种方式连接的信号的用户数据参数。
【解决方案2】:

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

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

【讨论】:

  • 感谢在我使用 --export-all-symbols之前帮助绑定我的信号
猜你喜欢
  • 1970-01-01
  • 2018-09-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-12-14
  • 1970-01-01
  • 1970-01-01
  • 2020-09-20
相关资源
最近更新 更多