SFML 和 GTK+ -GtkFileChooserDialog

发布于 2024-11-27 02:36:07 字数 372 浏览 0 评论 0原文

我正在使用 SFML 编写一个应用程序,我想使用 GTK+ 创建文件选择器对话框。我有这个代码:

gtk_init(&argc, &argv);

GtkWidget *dialog;
dialog = gtk_file_chooser_dialog_new ("Open file...", NULL, GTK_FILE_CHOOSER_ACTION_OPEN, GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL, GTK_STOCK_OPEN, GTK_RESPONSE_ACCEPT, NULL);
gtk_dialog_run (GTK_DIALOG (dialog));

并且对话框正在显示,但它没有被破坏:(

I'm writing an app using SFML and I want to use GTK+ to create file chooser dialogs. I have this code:

gtk_init(&argc, &argv);

GtkWidget *dialog;
dialog = gtk_file_chooser_dialog_new ("Open file...", NULL, GTK_FILE_CHOOSER_ACTION_OPEN, GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL, GTK_STOCK_OPEN, GTK_RESPONSE_ACCEPT, NULL);
gtk_dialog_run (GTK_DIALOG (dialog));

And the dialog is showing, but it doesn't get destroyed :(

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

半世晨晓 2024-12-04 02:36:07

gtk_dialog_run文档中有一条注释

gtk_dialog_run() 返回后,如果您愿意,您有责任隐藏或销毁对话框。

所以对话框不应该被自动销毁,程序员必须这样做。


EDIT:

另一个问题是您没有运行 GTK 主循环(gtk_main() 或其变体),因此 GTK 无法处理销毁小部件所需的事件(GTK 的任何部分都没有在该时间内运行)事件存在)。此问题的解决方案位于使用 gtk_idle_add( )gtk_main() 之后调用函数
被称为。在此函数中,显示对话框,将结果提供给调用者,销毁对话框并调用 gtk_main_quit() 来终止 GTK 主循环。

但是,gtk_idle_add() 在 GTK+2.6 中已被弃用,并且在 GTK+3.0 中不存在,因此应使用 g_idle_add() 来代替。你的代码可能是这样的

struct fch_result {
    gint response;
    // other information to return like filename,...
};

static gboolean fch_dialog(gpointer user_data)
{
    struct fch_result *result = (struct fch_result *) user_data;
    GtkWidget *dialog = gtk_file_chooser_dialog_new ( ... );
    result->response = gtk_dialog_run (GTK_DIALOG(dialog));
    // now add other information to result

    gtk_widget_destroy(dialog);
    gtk_main_quit();  // terminate the gtk_main loop called from caller
    return FALSE;
}

int main(int argc, char** argv)
{
    gtk_init(&argc, &argv);

    struct fch_result data;
    g_idle_add(fch_dialog, &data);

    gtk_main();

    // continue with the program
    return 0;
}

In the gtk_dialog_run documentation there is a note

After gtk_dialog_run() returns, you are responsible for hiding or destroying the dialog if you wish to do so.

So the dialog should not get destroyed automaticaly, the programmer must do it.


EDIT:

The another problem is that you are not running GTK main loop (gtk_main() or its variant) so the GTK can not deal with events necessary to destroy a widget (no part of GTK is running in the time the events are present). The sollution for this is in answer to another question using gtk_idle_add() to invoke function after gtk_main()
is called. In this function the dialog is shown, the result is given to the caller, the dialog is destroyed and gtk_main_quit() is called to terminate GTK main loop.

However, gtk_idle_add() is deprecated in GTK+2.6 and is not present in GTK+3.0, so g_idle_add() should be used instead. Your code could be somethink like

struct fch_result {
    gint response;
    // other information to return like filename,...
};

static gboolean fch_dialog(gpointer user_data)
{
    struct fch_result *result = (struct fch_result *) user_data;
    GtkWidget *dialog = gtk_file_chooser_dialog_new ( ... );
    result->response = gtk_dialog_run (GTK_DIALOG(dialog));
    // now add other information to result

    gtk_widget_destroy(dialog);
    gtk_main_quit();  // terminate the gtk_main loop called from caller
    return FALSE;
}

int main(int argc, char** argv)
{
    gtk_init(&argc, &argv);

    struct fch_result data;
    g_idle_add(fch_dialog, &data);

    gtk_main();

    // continue with the program
    return 0;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文