热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

gtk在对话框中显示一个对话框-gtkdisplayingadialogfromwithinadialog

Imwritingagtkprogram,atsomepointinmyapplication,Icallagtkdialogtoobtaininputfrom

I'm writing a gtk program, at some point in my application, I call a gtkdialog to obtain input from the user. what I want to do is close the current dialog and open another dialog when the OK button is clicked, I already have a function doing some other work, starting the dialog in that function would be great. here's the parent dialog code:

我正在编写一个gtk程序,在我的应用程序中的某个时刻,我调用gtkdialog来获取用户的输入。我想要做的是关闭当前对话框并在单击确定按钮时打开另一个对话框,我已经有一个功能做其他工作,在该功能中启动对话框会很棒。这是父对话框代码:

GtkWidget * new_button_dialog = gtk_dialog_new_with_buttons("new button",(GtkWindow *)container,GTK_DIALOG_DESTROY_WITH_PARENT,"OK", GTK_RESPONSE_OK,NULL);
       GtkWidget * content_area = gtk_dialog_get_content_area (GTK_DIALOG (new_button_dialog));
       GtkWidget * button_name_label = gtk_label_new ("Press escape at any time to cancel.\ntype in the button name");
       gtk_container_add (GTK_CONTAINER (content_area) , button_name_label);
       GtkWidget * button_name_entry = gtk_entry_new();
       gtk_entry_set_activates_default((GtkEntry*)button_name_entry,TRUE);
       gtk_container_add(GTK_CONTAINER(content_area), button_name_entry);
       GtkWidget * button_text_dialog = gtk_dialog_new_with_buttons("new button",(GtkWindow *)container,GTK_DIALOG_DESTROY_WITH_PARENT,"OK", GTK_RESPONSE_OK,NULL);
      gtk_container_add (GTK_CONTAINER (container), button);
       g_signal_connect(new_button_dialog,"response",G_CALLBACK (add_new_button),NULL);
gtk_signal_connect_object (GTK_OBJECT (button), "clicked",GTK_SIGNAL_FUNC (gtk_widget_show_all),(GtkWidget*)new_button_dialog);
       gtk_widget_show_all((GtkWidget *)container);

1 个解决方案

#1


2  

The response of a dialog cannot be handled in the same function that the dialog is created. It means that in one function you cannot create a dialog, handle its input, close that dialog and open a new dialog. It cannot be done since when that function exits the dialog entry is empty and the first dialog is just shown.

无法在创建对话框的同一函数中处理对话框的响应。这意味着在一个函数中,您无法创建对话框,处理其输入,关闭该对话框并打开新对话框。当该函数退出对话框条目为空并且仅显示第一个对话框时,无法完成。

It should be done as a chain of separate event handlers. All needed variables (data context) should be transferred between those separate handlers using a pointer to structure that holds that context.

它应该作为一系列独立的事件处理程序来完成。应使用指向保存该上下文的结构的指针在这些单独的处理程序之间传输所有需要的变量(数据上下文)。

The below full runnable example demonstrates such flow.

以下完整的可运行示例演示了此类流程。

In the fist dialog handler we want to do something with the main window and some container object and maybe we want some other data that is shared between widgets of that main window. The structure window_ctx_t is declared for such context. There is a boolean flag in that context to lock creation on new dialog chains before closing active dialogs.

在第一个对话框处理程序中,我们想要对主窗口和一些容器对象做一些事情,也许我们想要在该主窗口的窗口小部件之间共享一些其他数据。为此类上下文声明了结构window_ctx_t。在关闭活动对话框之前,该上下文中有一个布尔标志用于锁定新对话链上的创建。

The function first_dialog_create creates the first dialog with text entry field. It receives window_ctx_t, where the main window pointer is stored that is used for the dialog creation. Now we need to attach to that dialog response handler and provide to the handler enough data for its functionality. In that example we provide pointer to text entry that is used to read text.

函数first_dialog_create创建带有文本输入字段的第一个对话框。它接收window_ctx_t,其中存储用于对话创建的主窗口指针。现在我们需要附加到该对话框响应处理程序,并为处理程序提供足够的数据以实现其功能。在该示例中,我们提供指向用于读取文本的文本条目的指针。

Using provided data the response handler add_new_button is able to read text entry, to create a new dialog attached to the main window and also to do something with provided container.

使用提供的数据,响应处理程序add_new_button能够读取文本条目,创建一个附加到主窗口的新对话框,还可以使用提供的容器执行某些操作。

When the first dialog data context is not needed it is freed by calling g_free() in the dialog destroyed handler.

当不需要第一个对话框数据上下文时,通过在对话框销毁的处理程序中调用g_free()来释放它。

Build the example:

建立示例:

gcc -Wall -g dialogs.c -o dialogs `pkg-config --cflags gtk+-2.0` `pkg-config --libs gtk+-2.0`

Example "dialogs.c"

#include 

typedef struct {
    GtkWidget *container;
    GtkWindow *window;
    gboolean dialogs_created;
} window_ctx_t;

typedef struct {
    GtkEntry *entry;
    window_ctx_t *window_ctx;
} first_dialog_data_t;

static void first_dialog_create(gpointer context);

static void another_dialog_response(GtkWidget *dialog, gint response_id,
    gpointer ctx)
{
    window_ctx_t *win_ctx = ctx;

    gtk_widget_destroy(dialog);
    win_ctx->dialogs_created = FALSE;
}

static void cleanup_first_dialog(GtkWidget *widget, gpointer data)
{
    g_free(data);
}

/* response callback of the first dialog */
static void add_new_button(GtkWidget *dialog, gint response_id,
    gpointer ctx)
{
    GtkWidget *button;
    GtkWidget *button_text_dialog;
    first_dialog_data_t *data = ctx;

    if (response_id != GTK_RESPONSE_OK) {
        data->window_ctx->dialogs_created = FALSE;
        goto Exit;
    }

    /* Create a new button with the label from text entry */
    button = gtk_button_new_with_label(gtk_entry_get_text(data->entry));
    /* When the button receives the "clicked" signal, it will again open
     * "new button" dialog */
    g_signal_connect_swapped(button, "clicked", G_CALLBACK(first_dialog_create),
        data->window_ctx);
    /* add button to the container */
    gtk_container_add(GTK_CONTAINER(data->window_ctx->container), button);
    /* show button */
    gtk_widget_show(button);

    /* create another dialog */
    button_text_dialog = gtk_dialog_new_with_buttons("new button",
        data->window_ctx->window, GTK_DIALOG_DESTROY_WITH_PARENT, "OK",
        GTK_RESPONSE_OK, NULL);
    g_signal_connect(button_text_dialog, "response",
        G_CALLBACK(another_dialog_response), (gpointer)data->window_ctx);

    gtk_widget_show_all(button_text_dialog);

Exit:
    gtk_widget_destroy(dialog);
}

static void first_dialog_create(gpointer context)
{
    first_dialog_data_t *data;
    window_ctx_t *win_ctx = context;

    if (win_ctx->dialogs_created)
        return;

    /* lock buttons handler */
    win_ctx->dialogs_created = TRUE;

    /* allocate structure for dialog context */
    data = g_new0(first_dialog_data_t, 1);

    /* create first dialog, its label and text entry */
    GtkWidget *new_button_dialog = gtk_dialog_new_with_buttons("new button",
        win_ctx->window, GTK_DIALOG_DESTROY_WITH_PARENT,"OK",
        GTK_RESPONSE_OK, NULL);

    GtkWidget *content_area = gtk_dialog_get_content_area(
    GTK_DIALOG(new_button_dialog));
    GtkWidget *button_name_label = gtk_label_new("Press escape at any time to "
        "cancel.\ntype in the button name");
    gtk_container_add(GTK_CONTAINER(content_area), button_name_label);

    GtkWidget * button_name_entry = gtk_entry_new();
    gtk_entry_set_activates_default((GtkEntry*)button_name_entry,TRUE);
    gtk_container_add(GTK_CONTAINER(content_area), button_name_entry);

    /* provide data to response handler */
    data->entry = (GtkEntry *)button_name_entry;
    data->window_ctx = win_ctx;
    g_signal_connect(new_button_dialog, "response", G_CALLBACK(add_new_button),
        data);

    /* handler to free dialog context 'data' when destroed */
    g_signal_connect(new_button_dialog, "destroy",
        G_CALLBACK(cleanup_first_dialog), data);

    /* show first dialog */
    gtk_widget_show_all(new_button_dialog);
}

static void destroy(GtkWidget *widget, gpointer data)
{
    gtk_main_quit();
}

int main(int argc, char *argv[])
{
    GtkWidget *window;
    GtkWidget *box1;
    GtkWidget *button;
    window_ctx_t win_ctx;

    gtk_init (&argc, &argv);
    window = gtk_window_new(GTK_WINDOW_TOPLEVEL);

    box1 = gtk_hbox_new(FALSE, 0);
    gtk_container_add(GTK_CONTAINER(window), box1);

    /* set window context */
    win_ctx.window = (GtkWindow *)window;
    win_ctx.cOntainer= box1;
    win_ctx.dialogs_created = FALSE;

    g_signal_connect (window, "destroy", G_CALLBACK(destroy), NULL);
    gtk_container_set_border_width(GTK_CONTAINER(window), 10);

    button = gtk_button_new_with_label("Start");
    g_signal_connect_swapped(button, "clicked", G_CALLBACK(first_dialog_create),
        &win_ctx);
    gtk_container_add(GTK_CONTAINER(box1), button);

    gtk_widget_show_all(window);

    gtk_main();

    return 0;
}

推荐阅读
  • 不同优化算法的比较分析及实验验证
    本文介绍了神经网络优化中常用的优化方法,包括学习率调整和梯度估计修正,并通过实验验证了不同优化算法的效果。实验结果表明,Adam算法在综合考虑学习率调整和梯度估计修正方面表现较好。该研究对于优化神经网络的训练过程具有指导意义。 ... [详细]
  • 本文介绍了如何使用n3-charts绘制以日期为x轴的数据,并提供了相应的代码示例。通过设置x轴的类型为日期,可以实现对日期数据的正确显示和处理。同时,还介绍了如何设置y轴的类型和其他相关参数。通过本文的学习,读者可以掌握使用n3-charts绘制日期数据的方法。 ... [详细]
  • 本文介绍了brain的意思、读音、翻译、用法、发音、词组、同反义词等内容,以及脑新东方在线英语词典的相关信息。还包括了brain的词汇搭配、形容词和名词的用法,以及与brain相关的短语和词组。此外,还介绍了与brain相关的医学术语和智囊团等相关内容。 ... [详细]
  • 向QTextEdit拖放文件的方法及实现步骤
    本文介绍了在使用QTextEdit时如何实现拖放文件的功能,包括相关的方法和实现步骤。通过重写dragEnterEvent和dropEvent函数,并结合QMimeData和QUrl等类,可以轻松实现向QTextEdit拖放文件的功能。详细的代码实现和说明可以参考本文提供的示例代码。 ... [详细]
  • Java序列化对象传给PHP的方法及原理解析
    本文介绍了Java序列化对象传给PHP的方法及原理,包括Java对象传递的方式、序列化的方式、PHP中的序列化用法介绍、Java是否能反序列化PHP的数据、Java序列化的原理以及解决Java序列化中的问题。同时还解释了序列化的概念和作用,以及代码执行序列化所需要的权限。最后指出,序列化会将对象实例的所有字段都进行序列化,使得数据能够被表示为实例的序列化数据,但只有能够解释该格式的代码才能够确定数据的内容。 ... [详细]
  • 如何使用Java获取服务器硬件信息和磁盘负载率
    本文介绍了使用Java编程语言获取服务器硬件信息和磁盘负载率的方法。首先在远程服务器上搭建一个支持服务端语言的HTTP服务,并获取服务器的磁盘信息,并将结果输出。然后在本地使用JS编写一个AJAX脚本,远程请求服务端的程序,得到结果并展示给用户。其中还介绍了如何提取硬盘序列号的方法。 ... [详细]
  • Java容器中的compareto方法排序原理解析
    本文从源码解析Java容器中的compareto方法的排序原理,讲解了在使用数组存储数据时的限制以及存储效率的问题。同时提到了Redis的五大数据结构和list、set等知识点,回忆了作者大学时代的Java学习经历。文章以作者做的思维导图作为目录,展示了整个讲解过程。 ... [详细]
  • 展开全部下面的代码是创建一个立方体Thisexamplescreatesanddisplaysasimplebox.#Thefirstlineloadstheinit_disp ... [详细]
  • Java学习笔记之面向对象编程(OOP)
    本文介绍了Java学习笔记中的面向对象编程(OOP)内容,包括OOP的三大特性(封装、继承、多态)和五大原则(单一职责原则、开放封闭原则、里式替换原则、依赖倒置原则)。通过学习OOP,可以提高代码复用性、拓展性和安全性。 ... [详细]
  • 本文介绍了机器学习手册中关于日期和时区操作的重要性以及其在实际应用中的作用。文章以一个故事为背景,描述了学童们面对老先生的教导时的反应,以及上官如在这个过程中的表现。同时,文章也提到了顾慎为对上官如的恨意以及他们之间的矛盾源于早年的结局。最后,文章强调了日期和时区操作在机器学习中的重要性,并指出了其在实际应用中的作用和意义。 ... [详细]
  • 本文介绍了Swing组件的用法,重点讲解了图标接口的定义和创建方法。图标接口用来将图标与各种组件相关联,可以是简单的绘画或使用磁盘上的GIF格式图像。文章详细介绍了图标接口的属性和绘制方法,并给出了一个菱形图标的实现示例。该示例可以配置图标的尺寸、颜色和填充状态。 ... [详细]
  • 本文介绍了使用Spark实现低配版高斯朴素贝叶斯模型的原因和原理。随着数据量的增大,单机上运行高斯朴素贝叶斯模型会变得很慢,因此考虑使用Spark来加速运行。然而,Spark的MLlib并没有实现高斯朴素贝叶斯模型,因此需要自己动手实现。文章还介绍了朴素贝叶斯的原理和公式,并对具有多个特征和类别的模型进行了讨论。最后,作者总结了实现低配版高斯朴素贝叶斯模型的步骤。 ... [详细]
  • 本文总结了在编写JS代码时,不同浏览器间的兼容性差异,并提供了相应的解决方法。其中包括阻止默认事件的代码示例和猎取兄弟节点的函数。这些方法可以帮助开发者在不同浏览器上实现一致的功能。 ... [详细]
  • [echarts] 同指标对比柱状图相关的知识介绍及应用示例
    本文由编程笔记小编为大家整理,主要介绍了echarts同指标对比柱状图相关的知识,包括对比课程通过率最高的8个课程和最低的8个课程以及全校的平均通过率。文章提供了一个应用示例,展示了如何使用echarts制作同指标对比柱状图,并对代码进行了详细解释和说明。该示例可以帮助读者更好地理解和应用echarts。 ... [详细]
  • LINUX学习之centos7营救模式
    今天卸载软件的时候,不小心把GNOME的一些组件给卸了,导致桌面无法正常开启,会卡在启动过程中,而我的开机启动模式又是设置为图形界面,所以一开LINUX就卡住了,进入不了命令行界面 ... [详细]
author-avatar
faihiwang
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有