gpt4 book ai didi

c - GTK+ 段错误

转载 作者:行者123 更新时间:2023-11-30 17:52:24 34 4
gpt4 key购买 nike

这里完全是 100% 的编码菜鸟。我正在 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;
}

^^这是我的新代码。它编译并显示一个带有我选择的图像的窗口。

这里有人可以帮我编写代码,以便单击图像时应用程序关闭吗?

最佳答案

图像小部件是不捕获事件的小部件之一(因为它们没有自己的窗口)。您可以将此类小部件放置在事件框小部件中,并启用捕获按钮按下等事件。尝试更新的这个修改版本。

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

关于c - GTK+ 段错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16237736/

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