无法隐藏和显示小部件以响应事件

时间:2015-04-20 08:07:12

标签: c xml user-interface gtk3

我在使用删除事件关闭窗口小部件时遇到问题,通过gtk_widget_hide_on_delete无法通过交换的gtk_widget_show_all再次显示菜单窗口小部件,并将相应的窗口小部件作为参数传递给它。

我是Gtk + 3的新手,要了解它,我正在创建一个简单的程序,其中包含一个带有多个按钮小部件的选择菜单。按一个将隐藏菜单,并显示包含标签和TextEntry的窗口小部件,此窗口小部件的删除事件将隐藏窗口小部件,并再次显示菜单。

我已经创建了此代码的最小示例。 c文件简单地加载一个XML文件,其中包含显示和隐藏小部件所需的所有函数调用。

我依赖gtk_widget_show_all,gtk_widget_hide和gtk_widget_hide_on_delete,但是当我使用delete-event关闭练习小部件时,窗口会消失,但菜单小部件不会显示。

c代码简单地加载XML文件,显示菜单小部件并进入主gtk循环。

#include <gtk/gtk.h>
#include <stdio.h>

int main(int argc, char **argv)
{
    GtkBuilder * builder;
    GtkWidget * menu;
    GError *error = NULL;

    gtk_init(&argc, &argv);
    builder = gtk_builder_new();
    if (!gtk_builder_add_from_file(builder, "example.glade", &error)) {
        g_warning("%s", error->message);
        g_free(error);
        fprintf(stderr, "Failed to load build file");
    }

    menu = (GtkWidget *) gtk_builder_get_object(builder, "menu");

    gtk_builder_connect_signals(builder, NULL);
    gtk_widget_show(menu);

    gtk_main();

    return 0;
}

使用Glade创建了XML文件example.glade。

<?xml version="1.0" encoding="UTF-8"?>
<!-- Generated with glade 3.16.1 -->
<interface>
  <requires lib="gtk+" version="3.10"/>
  <object class="GtkWindow" id="exercise">
    <property name="can_focus">False</property>
    <property name="window_position">center</property>
    <signal name="delete-event" handler="gtk_widget_hide_on_delete" swapped="no"/>
    <signal name="delete-event" handler="gtk_widget_show_all" object="menu" after="yes" swapped="yes"/>
    <child>
      <object class="GtkGrid" id="grid2">
        <property name="visible">True</property>
        <property name="can_focus">False</property>
        <child>
          <object class="GtkEntry" id="entry1">
            <property name="visible">True</property>
            <property name="can_focus">True</property>
            <property name="hexpand">True</property>
          </object>
          <packing>
            <property name="left_attach">0</property>
            <property name="top_attach">1</property>
            <property name="width">1</property>
            <property name="height">1</property>
          </packing>
        </child>
        <child>
          <object class="GtkLabel" id="label1">
            <property name="visible">True</property>
            <property name="can_focus">False</property>
            <property name="hexpand">True</property>
            <property name="vexpand">True</property>
            <property name="label" translatable="yes">A window where stuff will happen.</property>
          </object>
          <packing>
            <property name="left_attach">0</property>
            <property name="top_attach">0</property>
            <property name="width">1</property>
            <property name="height">1</property>
          </packing>
        </child>
      </object>
    </child>
  </object>
  <object class="GtkWindow" id="menu">
    <property name="can_focus">False</property>
    <property name="window_position">center</property>
    <signal name="destroy" handler="gtk_main_quit" swapped="no"/>
    <child>
      <object class="GtkGrid" id="grid1">
        <property name="visible">True</property>
        <property name="can_focus">False</property>
        <child>
          <object class="GtkButton" id="button1">
            <property name="label" translatable="yes">button1</property>
            <property name="visible">True</property>
            <property name="can_focus">True</property>
            <property name="receives_default">True</property>
            <property name="hexpand">True</property>
            <property name="vexpand">True</property>
            <signal name="clicked" handler="gtk_widget_hide" object="menu" swapped="yes"/>
            <signal name="clicked" handler="gtk_widget_show_all" object="exercise" swapped="yes"/>
          </object>
          <packing>
            <property name="left_attach">0</property>
            <property name="top_attach">0</property>
            <property name="width">1</property>
            <property name="height">1</property>
          </packing>
        </child>
        <child>
          <object class="GtkButton" id="button2">
            <property name="label" translatable="yes">button2</property>
            <property name="visible">True</property>
            <property name="can_focus">True</property>
            <property name="receives_default">True</property>
            <property name="hexpand">True</property>
            <property name="vexpand">True</property>
            <signal name="clicked" handler="gtk_widget_hide" object="menu" swapped="yes"/>
            <signal name="clicked" handler="gtk_widget_show_all" object="exercise" swapped="yes"/>
          </object>
          <packing>
            <property name="left_attach">0</property>
            <property name="top_attach">1</property>
            <property name="width">1</property>
            <property name="height">1</property>
          </packing>
        </child>
      </object>
    </child>
  </object>
</interface>

1 个答案:

答案 0 :(得分:2)

问题在于你的Glade文件的这两行:

<signal name="delete-event" handler="gtk_widget_hide_on_delete" swapped="no"/>
<signal name="delete-event" handler="gtk_widget_show_all" object="menu" after="yes" swapped="yes"/>

查看gtk_widget_hide_on_delete()的{​​{3}},我们看到:

  

效用函数;旨在连接到GtkWindow上的“删除事件”信号。该函数在其参数上调用gtk_widget_hide(),然后返回TRUE

如果我们查看manual page信号,我们会看到:

  

返回

     

TRUE阻止为该事件调用其他处理程序。 FALSE进一步宣传该事件。

这意味着在调用gtk_widget_hide_on_delete()之后,将返回TRUE,这会阻止GTK运行时调用gtk_widget_show_all()

要实现您的目标,您可以使用自定义处理程序,如下所示:

  1. 删除这两行并替换为以下内容:

    <signal name="delete-event" handler="my_custom_func" object="menu" swapped="no"/>
    
  2. 将自定义处理程序添加到C文件中:

    G_MODULE_EXPORT gboolean
    my_custom_func(GtkWidget *w, GdkEvent *e, gpointer u) {
        gtk_widget_hide(w);
        gtk_widget_show_all(GTK_WIDGET(u));
        return TRUE;
    }