如何从GtkFileChooserDialog中选择文件?

时间:2014-03-23 15:43:19

标签: c gtk

我在Glade中创建了一个filechooser对话框。我在其中放置了OK和Cancel按钮。之后,我将GtkButton的点击处理程序设置为open_clicked或其他;我还有一个显示filechooser对话框的按钮。它的信号处理程序是cb_show_filed。这是我的代码:

#include <gtk/gtk.h>
typedef struct _Data Data;
struct _Data
{
    GtkWidget *file;
};
G_MODULE_EXPORT void cb_show_filed(GtkButton *button, Data *data)
{
    gtk_dialog_run(GTK_DIALOG(data->file));
    gtk_widget_hide(data->file);
}
int main(int argc, char **argv)
{
    GtkBuilder *builder;
    GtkWidget *window;
    GtkWidget *filechooserdialog1;
    gtk_init(&argc, &argv);
    builder = gtk_builder_new();
    gtk_builder_add_from_file(builder, "ui.glade", NULL);
    window = GTK_WIDGET(gtk_builder_get_object(builder, "window1"));
    g_signal_connect(window, "destroy", G_CALLBACK(gtk_main_quit), NULL);
    filechooser =
        GTK_WIDGET(gtk_builder_get_object(builder, "filechooserdialog1"));
    gtk_builder_connect_signals(builder, &data);
    g_object_unref(G_OBJECT(builder));
    gtk_widget_show(window);
    gtk_main();
    return (0);
}

现在,我如何从filechooserdialog1获取路径和文件名?

1 个答案:

答案 0 :(得分:1)

使用gtk_file_chooser_get_file()和朋友:

GFile *chosen_file = gtk_file_chooser_get_file(GTK_FILE_CHOOSER(filechooser));