所以我有这样的代码,我的主要

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));
}


因此,现在我想添加此部分以显示对话框,以便可以检测何时按下“打开”和“取消”按钮。在Glade中,我将“打开”按钮的响应ID设置为-5,将“取消”设置为-6,这与GTK_RESPONSE_OK和GTK_RESPONSE_CANCEL对应,但是当我按“打开”按钮时,我不会执行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));

最佳答案

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 );

09-08 07:50