所以我的代码就像这样,我的主要
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));
答案 0 :(得分:1)
GtkFileChooserDialog
是GtkDialog
的子类,因此使用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 );