gpt4 book ai didi

c - gtk 从对话框中显示对话框

转载 作者:IT王子 更新时间:2023-10-29 00:35:31 28 4
gpt4 key购买 nike

我正在写一个 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);

最佳答案

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

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

下面的完整可运行示例演示了这种流程。

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

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

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

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

构建示例:

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

示例“dialogs.c”

#include <gtk/gtk.h>

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

关于c - gtk 从对话框中显示对话框,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33247405/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com