0
votes

I'm using GTK+ 3.10 with C under Fedora 20 to show a window where the user can select an option from a combo box, then press a button which will show a window with the selected option and an image. (I've used Glade to design the user interface.) The handler function for the button is

void on_btnDisplay_clicked(GObject *object, gpointer data)
{
    char *selectedText = gtk_combo_box_text_get_active_text(GTK_COMBO_BOX_TEXT(listBox));
    char imgPath[50];
    memset(imgPath, 0, sizeof(imgPath));

    sprintf(imgPath, "images/%s.png", selectedText);

    gtk_label_set_text(GTK_LABEL(lblText), selectedText);
    gtk_image_set_from_file(GTK_IMAGE(imgBox), imgPath);

    gtk_widget_show(wndDisplay);
}

given listBox, lblText, imgBox and wndDisplay are globals of type GtkWidget* pointing to widgets in my UI. There is no handler for destroying wndDisplay.

This works perfectly the first time I try it in a run, but if I close the display window, select something different and try again, it fails, showing several errors in the console (but no segmentation fault) - three of the form "Gtk-CRITICAL **: ... assertion failed" and two "GLib-GObject-WARNING **: invalid undeclared pointer in cast".

Why does this happen the second time I try to show the window? My guess is that I have to handle the destroy signal from the window in some way, but how?

2

2 Answers

0
votes

If you want to destroy your "second window", just use

  g_signal_connect_swapped (wndDisplay, "destroy",
                            G_CALLBACK (gtk_widget_destroy), wndDisplay)

and you are back to your main window, if that's your problem is. Btw, why you are using memset and sprintf again?

0
votes

I've managed to work out that I have to hide the window on closing rather than destroy it. So using

gtk_widget_hide(wndDisplay)

to handle the delete event of the window will do the trick.