2014-03-30 60 views
0

所以,我的代碼,這樣下去callig新功能,我的主要如何在GTK連接的g_signal_connect而不G_CALLBACK()

int main(int argc, char **argv) 
{ 
button = GTK_WIDGET(gtk_builder_get_object(builder, "button2")); 
g_signal_connect(button, "clicked", G_CALLBACK (show_dialog), NULL); 
} 

然後,我有show_dialog這是這樣的

static void 
show_dialog (GtkWidget *button, 
      gint  response_id, 
      gpointer user_data) 
{ 
GtkBuilder *builder; 
GtkWidget *filechoosedialog; 

builder = gtk_builder_new(); 
gtk_builder_add_from_file(builder, GLADE_FILE, NULL); 
fiilechoosedialog = GTK_WIDGET(gtk_builder_get_object( 
           builder, 
           "filechooserdialog1")); 
/* Run dialog */ 
gtk_dialog_run(GTK_DIALOG(filechoosedialog)); 
gtk_widget_hide(filechoosedialog); 

gtk_builder_connect_signals(builder, NULL); 

g_object_unref(G_OBJECT(builder)); 
} 

所以現在我想補充這部分顯示對話框,所以我可以打開時檢測和取消按鈕被按下。格萊德我seted打開按鈕的響應ID爲-5和取消是-6其corespond到GTK_RESPONSE_OK和GTK_RESPONSE_CANCEL但是當我按OPEN鍵我沒有得到執行了g_print

switch (response_id) 
{ 
    case GTK_RESPONSE_OK: 

     g_print ("Selected filename: %s\n", filename); 
     g_print ("response idd: %d\n", response_id); 
    break; 
    default: 

    break; 
} 

gtk_widget_destroy(GTK_WIDGET(filechoosedialog) );

回答

1

GtkFileChooserDialogGtkDialog的子類,所以使用gint gtk_dialog_run(GtkDialog *dialog);應該可以工作,它將返回響應代碼。


// get hold of the return value of `gtk_run_dialog(...)`, lookup the type 
response_id = gtk_dialog_run(GTK_DIALOG(filechoosedialog)); 
switch (response_id) 
{ 
    case GTK_RESPONSE_OK: 
     g_print ("Selected filename: %s\n", filename); 
     g_print ("response idd: %d\n", response_id); 
    break; 
    default: 
     g_print ("do'h! no file selected!\n"); 
    break; 
} 

// I am pretty sure you do not need this, this approach does not use the `updated` signal 
gtk_builder_connect_signals(builder, NULL); 
+0

但它不工作,我什麼也得不到的RESPONSE_ID。我得到的文件名,但對話框不會發出RESPONSE_ID雖然我seted了按鍵響應ID爲-5就OK和-6爲取消 – Vido

+0

能否請更新您的代碼(請說comeplete和可編譯),目前還不清楚到底是什麼從你說的話會錯或者你希望顯示的響應代碼或您放置'gtk_dialog_run' - 這是一個重要的延遲。 – drahnr

+0

還有就是我的代碼更新是現在更加清晰,TNX @drahnr對你有所幫助 – Vido