0

utter total 100% coding noob here. I am experimenting in GTK+ and C and I'm trying to create a basic window which has an image over it.

This is my (probably cringeworthy) code:

#include <gtk/gtk.h>


int main( int argc, char *argv[])
{
  GtkWidget *window;
  GtkWidget *fixed;
  GtkWidget *button;
  GtkImage *image;

  gtk_init(&argc, &argv);

  window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
  gtk_window_set_title(GTK_WINDOW(window), "GtkButton");
  gtk_window_set_default_size(GTK_WINDOW(window), 640, 480);
  gtk_window_set_position(GTK_WINDOW(window), GTK_WIN_POS_CENTER);

  fixed = gtk_fixed_new();
  gtk_container_add(GTK_CONTAINER(window), fixed);

  image = gtk_image_new();
  gtk_image_set_from_file(GTK_IMAGE(image),"/home/testbed/Downloads/efnbxw.jpg");

  gtk_fixed_put(GTK_FIXED(fixed), button, 50, 50);
  gtk_widget_set_size_request(button, 80, 35);

  g_signal_connect(G_OBJECT(button), "clicked",
      G_CALLBACK(gtk_main_quit), G_OBJECT(window));

  g_signal_connect_swapped(G_OBJECT(window), "destroy",
      G_CALLBACK(gtk_main_quit), NULL);

  gtk_widget_show_all(window);

  gtk_main();

  return 0;
}

What did I do wrong?

Scrap the above code, it's just entirely incorrect

#include <gtk/gtk.h>

int main( int argc, char *argv[])
{
    GtkWidget *window, *image;

    gtk_init(&argc, &argv);

    window = gtk_window_new(GTK_WINDOW_TOPLEVEL);

    gtk_window_set_position(GTK_WINDOW(window), GTK_WIN_POS_CENTER);
    gtk_window_set_default_size(GTK_WINDOW(window), 230, 150);
    gtk_window_set_title(GTK_WINDOW(window), "Image");
    gtk_window_set_resizable(GTK_WINDOW(window), FALSE);

    gtk_container_set_border_width(GTK_CONTAINER(window), 2);

    image = gtk_image_new_from_file("/home/testbed/Downloads/efnbxw.jpg");
    gtk_container_add(GTK_CONTAINER(window), image);

    g_signal_connect(G_OBJECT(window), "destroy", G_CALLBACK(gtk_main_quit), NULL);
    gtk_widget_show_all(window);
    gtk_main();

    return 0;
}

^^Heres my new code. It compiles and displayed a window with my selected image.

Can anyone here help me code this so that when the image is clicked, the application closes?

4

2 回答 2

1

Image widgets are one of those that don't capture events (because they don't have their own window). You can place such widgets in an eventbox widget and enable the capture of events such as button presses. Try this modified version of your update.

#include <gtk/gtk.h>

int main( int argc, char *argv[])
{
    GtkWidget *window, *image;

    gtk_init(&argc, &argv);

    window = gtk_window_new(GTK_WINDOW_TOPLEVEL);

    gtk_window_set_position(GTK_WINDOW(window), GTK_WIN_POS_CENTER);
    gtk_window_set_default_size(GTK_WINDOW(window), 230, 150);
    gtk_window_set_title(GTK_WINDOW(window), "Image");
    gtk_window_set_resizable(GTK_WINDOW(window), FALSE);

    gtk_container_set_border_width(GTK_CONTAINER(window), 2);

    image = gtk_image_new_from_file("/home/testbed/Downloads/efnbxw.jpg");
    //gtk_container_add(GTK_CONTAINER(window), image);

    g_signal_connect(G_OBJECT(window), "destroy", G_CALLBACK(gtk_main_quit), NULL);

    GtkWidget* eventBox = gtk_event_box_new();
    gtk_container_add(GTK_CONTAINER(window), eventBox);

    // Capture button presses.                                                                                                                     
    gtk_widget_add_events (eventBox, GDK_BUTTON_PRESS_MASK);
    g_signal_connect(G_OBJECT(eventBox), "button-press-event", G_CALLBACK(gtk_main_quit), NULL);
    gtk_container_add(GTK_CONTAINER(eventBox), image);

    gtk_widget_show_all(window);
    gtk_main();

    return 0;
}
于 2013-04-26T20:39:50.347 回答
-1

Segmenation fault means that you are accesing memory positions that are out of the bounds defined in your program.From the above code i assume that one of your pointers causes the problem..

于 2013-04-26T13:39:43.273 回答