信号'show'不是从用GtkBuilder构建的GUI发出的?

时间:2017-10-16 14:56:21

标签: c signals gtk3 gtkbuilder

我试图通过'show'信号的信号处理程序从GSettings初始化一个GtkEntry小部件。这在手动构建用户界面时按预期工作,但在使用GtkBuilder时无法工作。这同样适用于其他GtkWidget信号; GtkEntry信号按预期工作。看起来信号永远不会发出?

#include <gtk/gtk.h>


void activated (GtkEntry *entry,
               gpointer  user_data){

    GtkWidget *dialog = gtk_message_dialog_new(GTK_WINDOW(user_data), GTK_DIALOG_DESTROY_WITH_PARENT,
                                               GTK_MESSAGE_INFO, GTK_BUTTONS_CLOSE,
                                               gtk_entry_get_text(entry));
    gtk_dialog_run(GTK_DIALOG(dialog));
    gtk_widget_destroy(dialog);

}



void shown (GtkWidget *widget,
               gpointer   user_data){
    GtkWidget *dialog = gtk_message_dialog_new(GTK_WINDOW(user_data), GTK_DIALOG_DESTROY_WITH_PARENT,
                                               GTK_MESSAGE_INFO, GTK_BUTTONS_CLOSE,
                                               "SHOWN!");
    gtk_dialog_run(GTK_DIALOG(dialog));
    gtk_widget_destroy(dialog);

}



int main(int argc, char **argv){
    GtkWidget *window1, *window2, *entry;
    GtkBuilder *builder;


    gtk_init(&argc, &argv);

    // Construct working window 1

    window1 = gtk_window_new(GTK_WINDOW_TOPLEVEL);
    gtk_window_set_title(GTK_WINDOW(window1),"Working");
    entry=gtk_entry_new();

    g_signal_connect(entry, "activate", G_CALLBACK(activated), window1);
    g_signal_connect(entry, "show", G_CALLBACK(shown), window1);

    gtk_container_add(GTK_WINDOW(window1), entry);


    // Construct not working window 2

    builder=gtk_builder_new_from_file ("window.glade");
    window2 = GTK_WIDGET(gtk_builder_get_object(builder, "window2"));
    gtk_builder_connect_signals(builder, NULL);
    g_object_unref(builder);


    gtk_widget_show_all(window1);
    gtk_widget_show_all(window2);


    gtk_main();

    return 0;
}

window.glade:

<?xml version="1.0" encoding="UTF-8"?>
<!-- Generated with glade 3.18.3 -->
<interface>
  <requires lib="gtk+" version="3.12"/>
  <object class="GtkWindow" id="window2">
    <property name="can_focus">False</property>
    <property name="title" translatable="yes">Not working</property>
    <child>
      <object class="GtkEntry" id="entry1">
        <property name="visible">True</property>
        <property name="can_focus">True</property>
        <property name="shadow_type">none</property>
        <signal name="activate" handler="activated" object="window2" swapped="no"/>
        <signal name="show" handler="shown" object="window2" swapped="no"/>
      </object>
    </child>
  </object>
</interface>

1 个答案:

答案 0 :(得分:1)

GLADE中的可见标志设置为false(取消),以便当GtkBuilder实例化glade文件内容时,不显示该小部件;然后,当您为gtk_widget_show_all window2时,信号将被激活。

enter image description here

或者,修改 window.glade 并将visible属性设置为False或将其删除(我认为默认情况下窗口小部件不可见),例如:

 ...
 <object class="GtkEntry" id="entry1">
    <property name="visible">False</property>
 ...