我有一个试图执行以下操作的应用程序:
GTK2
顶级主窗口GtkImages
矩阵,用于显示动画GIFS
和静态JPEGS
JPEGS
将填充矩阵GIFS
JPEGS
仅当两个或多个随机选择的JPEGS
放置在矩阵的一行中时,才会发生运行时错误。
以下是此类运行时错误的示例:
(wrong:3909): Gtk-WARNING **: Can't set a parent on widget which has a parent
如果该行的每个图像都是唯一的,则不会发生运行时错误。
代码片段和运行时输出如下:
/*
* Compile me with:
* gcc -Wall -o wrong wrong.c $(pkg-config --cflags --libs gtk+-2.0 gmodule-2.0)
*/
/* header includes */
/**** prototypes ****/
/********************/
typedef struct
{
unsigned int pixel_width, pixel_height;
gchar fileName[20];
GtkWidget *image;
}symbol_t;
symbol_t symbols[] =
{
{ 118, 107, "images/LO.jpg", NULL },
{ 118, 107, "images/L1.jpg", NULL },
{ 118, 107, "images/L2.jpg", NULL },
{ 118, 107, "images/L3.jpg", NULL },
{ 118, 107, "images/H1.jpg", NULL },
{ 118, 107, "images/H2.jpg", NULL },
{ 118, 107, "images/H3.jpg", NULL },
{ 118, 107, "images/H4.jpg", NULL },
{ 118, 107, "images/H5.jpg", NULL }
};
GtkWidget *frame; /* for absolute positioning of widgets */
GtkWidget *window;
int Init( void )
{
/* initialize random number generator */
}
static void destroy (GtkWidget *window, gpointer data)
{
gtk_main_quit ();
}
GtkWidget *SetupWindow(gchar *data, const gchar *filename)
{
/* setup top-level window setting the background to the image contained
in *filename and return window widget
*/
return(window);
}
int main (int argc, char *argv[])
{
unsigned int y, i, pos_x, pos_y;
gtk_init (&argc, &argv);
Init(); // init random number generator
window = SetupWindow("Broken", "images/background.jpg");
g_signal_connect (G_OBJECT (window), "destroy", G_CALLBACK (destroy), NULL);
frame = gtk_fixed_new();
gtk_container_add(GTK_CONTAINER(window), frame);
/* setup symbol jpgs */
for( i = 0; i < 9; i++ )
{
/* load each symbol image into memory */
symbols[i].image = gtk_image_new_from_file( symbols[i].fileName );
}
/* display some symbols */
pos_y = 150;
pos_x = 187;
for( y = 0; y < 5 ; y++ ) /* first row - 5 symbols */
{
i = (unsigned int)(random()%9);
printf("Symbol[%d] [%s]\n", i, symbols[i].fileName);
gtk_fixed_put(GTK_FIXED(frame), symbols[i].image, pos_x, pos_y);
pos_x += symbols[i].pixel_width;
}
gtk_widget_show_all(window);
gtk_main ();
return 0;
}
在行上放置两个或多个匹配符号(图像)时出现运行时错误:
[chim] ~/source/matrix > ./wrong Symbol[1] [images/L1.jpg] Symbol[7] [images/H4.jpg] Symbol[0] [images/LO.jpg] Symbol[7] [images/H4.jpg] (wrong:3909): Gtk-WARNING **: Can't set a parent on widget which has a parent Symbol[5] [images/H2.jpg] (wrong:3909): Gtk-CRITICAL **: gtk_widget_destroy: assertion `GTK_IS_WIDGET (widget)' failed (wrong:3909): Gtk-CRITICAL **: gtk_widget_destroy: assertion `GTK_IS_WIDGET (widget)' failed
发生这种情况时,行中的某些图像为空(白色)。
当行上没有匹配的符号(图像)时输出:
[chim] ~/source/matrix > ./wrong Symbol[1] [images/L1.jpg] Symbol[6] [images/H3.jpg] Symbol[3] [images/L3.jpg] Symbol[0] [images/LO.jpg] Symbol[4] [images/H1.jpg]
在这种情况下,所有图像都会正确显示。
有关如何解决以及我可能做错的任何建议?
答案 0 :(得分:1)
将图像放入另一个窗口小部件后,它将由该(父)窗口小部件拥有和管理 - 您无法将其添加到多个窗口小部件中。
使其工作的简单方法是每次要将图像添加到窗口时加载gtk_image_new_from_file()
图像。如果您不想这样做,也许您可以使用gtk_image_new_from_image()
之类的内容复制图像,然后再将其添加到窗口小部件中。