gpt4 book ai didi

c - 对话框无法第二次打开。 GTK+、Glade 和 C

转载 作者:行者123 更新时间:2023-11-30 15:31:57 25 4
gpt4 key购买 nike

我正在使用 GTK+ 和 Glade 设计一个界面。我使用glade 创建了几个对话框,并通过C 代码进行连接。它们第一次工作得很好,但是当用户尝试重复使用它时,程序就会停止工作。命令窗口给出

(program_name.exe:1668): GLib-GObject-Warning **: invalid (NULL) pointer instance
(program_name.exe:1688): GLib-Gobject-Critical **: g_signal_connect_data: assertion 'G_TYPE_CHECK_INSTANCE (instance)' failed
(program)name.exe:1668): Gtk-CRITICAL **: gtk_widget_show: assertion 'GTK_IS_WIDGET(widget)' failed

这是代码

GtkWidget *entry_obs_cells[12];
void
on_observation_cells_activate(GtkWidget *widget, gpointer user_data){
GtkWidget *observation_cells;
observation_cells = glade_xml_get_widget (gxml, "dialog3_obs_cells");
GtkWidget *label1, *label2, *label3, *label4, *label5, *label6, *label7, *label8, *label9, *label10, *time_interval;
label1 = glade_xml_get_widget(gxml, "label1");
label2 = glade_xml_get_widget(gxml, "label2_obs_cells");
label3 = glade_xml_get_widget(gxml, "label3_obs_cells");
label4 = glade_xml_get_widget(gxml, "label4_obs_cells");
label5 = glade_xml_get_widget(gxml, "label5_obs_cells");
label6 = glade_xml_get_widget(gxml, "label6_obs_cells");
label7 = glade_xml_get_widget(gxml, "label7_obs_cells");
label8 = glade_xml_get_widget(gxml, "label8_obs_cells");
label9 = glade_xml_get_widget(gxml, "label9_obs_cells");
label10 = glade_xml_get_widget(gxml, "label10_obs_cells");


GtkWidget *button1,*button2;
button1 = glade_xml_get_widget(gxml,"Save_obs_cells");
button2 = glade_xml_get_widget(gxml,"Cancel_obs_cells");

entry_obs_cells[1] = glade_xml_get_widget(gxml, "entry1_obs_cells");
entry_obs_cells[2] = glade_xml_get_widget(gxml, "entry2_obs_cells");
entry_obs_cells[3] = glade_xml_get_widget(gxml, "entry3_obs_cells");
entry_obs_cells[4] = glade_xml_get_widget(gxml, "entry4_obs_cells");
entry_obs_cells[5] = glade_xml_get_widget(gxml, "entry5_obs_cells");
entry_obs_cells[6] = glade_xml_get_widget(gxml, "entry6_obs_cells");
entry_obs_cells[7] = glade_xml_get_widget(gxml, "entry7_obs_cells");
entry_obs_cells[8] = glade_xml_get_widget(gxml, "entry8_obs_cells");
entry_obs_cells[9] = glade_xml_get_widget(gxml, "entry9_obs_cells");
entry_obs_cells[10] = glade_xml_get_widget(gxml, "entry10_obs_cells");
entry_obs_cells[11] = glade_xml_get_widget(gxml, "entry_obs_cells");
entry_obs_cells[12] = glade_xml_get_widget(gxml, "entry12_obs_cells");

gtk_widget_show (observation_cells);
g_signal_connect(GTK_BUTTON(button1), "clicked", G_CALLBACK(on_Save_obs_cells_activate), observation_cells);
g_signal_connect(GTK_BUTTON(button2), "clicked", G_CALLBACK(on_Cancel_obs_cells_activate), NULL);

}

on_Save_obs_cells_activate()
{
char *record_results=(char*)malloc(500);
strcpy(record_results,project_dir);
strcat(record_results,"\\Record_results.txt");
printf("\n%s\n",record_results);
FILE *file;
file = fopen(record_results, "w+");
fprintf(file, "%s\n%s\n%s\n%s\n%s\n%s\n%s\n%s\n%s\n%s\n%s\n%s\n", gtk_entry_get_text(GTK_ENTRY(entry_obs_cells[11])),gtk_entry_get_text(GTK_ENTRY(entry_obs_cells[11])),gtk_entry_get_text(GTK_ENTRY(entry_obs_cells[1])),gtk_entry_get_text(GTK_ENTRY(entry_obs_cells[2])),gtk_entry_get_text(GTK_ENTRY(entry_obs_cells[3])),gtk_entry_get_text(GTK_ENTRY(entry_obs_cells[4])),gtk_entry_get_text(GTK_ENTRY(entry_obs_cells[5])),gtk_entry_get_text(GTK_ENTRY(entry_obs_cells[6])),gtk_entry_get_text(GTK_ENTRY(entry_obs_cells[7])),gtk_entry_get_text(GTK_ENTRY(entry_obs_cells[8])),gtk_entry_get_text(GTK_ENTRY(entry_obs_cells[9])),gtk_entry_get_text(GTK_ENTRY(entry_obs_cells[10])));
fclose(file);

}

void
on_Cancel_obs_cells_activate (GtkButton *button, gpointer dialog)
{
gtk_widget_destroy(dialog);
}

最佳答案

正如 mfro 上面所说,您需要调用 gtk_widget_hide() 而不是 gtk_widget_destroy() 您还需要连接 delete-event当您单击右上角的关闭按钮时,向 gtk_widget_hide_on_delete() 发送信号以阻止窗口被销毁。

Glade XML 已弃用,已被 GtkBuilder 取代它包含在 Gtk 本身中。使用搜索和替换将glade_xml函数调用更改为相应的GtkBuilder函数应该很容易。如果您在 Glade 中创建了 xml 接口(interface)描述,它们将可以毫无问题地与 GtkBuilder 一起使用。

关于c - 对话框无法第二次打开。 GTK+、Glade 和 C,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24483957/

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