2017-10-16 86 views
2

我想通過一個'顯示'信號的信號處理程序從GSettings初始化一個GtkEntry小部件。這在手動構建用戶界面時按預期工作,但在使用GtkBuilder時無法工作。這同樣適用於其他GtkWidget信號; GtkEntry信號按預期工作。它看起來像信號永遠不會發射?使用GtkBuilder構建的GUI不會發出信號'show'?

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

剛一說明。據我所知,將** map **事件用於擬用於演出的目的會更好。這與** show **一模一樣,不需要改變空地中的任何東西。並且節目被稱爲「不,不是真的,這是來自 之前的那些奇怪的歷史事物之一」。你可以查看這個鏈接https://blogs.gnome.org/jnelson/2010/10/13/those-realize-map-widget-signals/ – likern

回答

1

設置可見標誌GLADE爲false(勾去掉)使得當GtkBuilder實例glade文件內容,未示出的工具;那麼當你對window2gtk_widget_show_all時,信號就會消失。

enter image description here

另外,編輯window.glade和Visible屬性設爲False或刪除它(我覺得默認的窗口小部件是不可見的),例如:

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

適合我,謝謝! – eezacque