这里总共100%编码noob。我正在试验GTK +和C,我正在尝试创建一个基本窗口,上面有一个图像。
这是我的(可能是令人讨厌的)代码:
#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;
}
我做错了什么?
废弃上述代码,这完全不正确
#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;
}
^^继承我的新代码。它使用我选择的图像编译并显示一个窗口。
这里的任何人都可以帮我编码,以便在点击图片时关闭应用程序吗?
答案 0 :(得分:1)
图像窗口小部件是不捕获事件的窗口小部件之一(因为它们没有自己的窗口)。您可以将这些小部件放在事件框小部件中,并启用按钮按下等事件的捕获。试试这个修改版本的更新。
#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;
}
答案 1 :(得分:-1)
分段错误意味着您正在访问超出程序中定义的边界的内存位置。从上面的代码中我假设您的一个指针导致问题..