2

我正在尝试通过“显示”信号的信号处理程序从 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>
4

1 回答 1

1

将GLADE中的 visible 标志设置为 false(取消勾选),这样当 GtkBuilder 实例化 glade 文件内容时,不会显示小部件;然后,当您执行gtk_widget_show_allfor时window2,信号将被发射。

在此处输入图像描述

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

 ...
 <object class="GtkEntry" id="entry1">
    <property name="visible">False</property>
 ...
于 2017-10-16T21:39:09.763 回答