0

我在整理一些旧的杂乱代码中的铸件时遇到问题,试图将其更新。它有这样的代码:

static void image_init(CtkImage *image)
{
    priv->texture = clutter_texture_new ();
    ...
}

static void refresh_icon (CtkImage *image)
{
  CtkImagePrivate *priv = image->priv;
  gtk_clutter_texture_set_from_pixbuf (CLUTTER_TEXTURE (priv->texture), priv->pixbuf, NULL);
}

这会产生这个编译时错误:

error: passing argument 1 of ‘gtk_clutter_texture_set_from_pixbuf’ from incompatible pointer type [-Werror]
/usr/include/clutter-gtk-1.0/clutter-gtk/gtk-clutter-texture.h:99:17: note: expected ‘struct GtkClutterTexture *’ but argument is of type ‘struct ClutterTexture *’

我认为我可以通过使用 GTK_CLUTTER_TEXTURE 来修复它,这确实可以编译,但是存在运行时错误并且缺少 pixbufs:

gtk_clutter_texture_set_from_pixbuf (GTK_CLUTTER_TEXTURE (texture), tiled, NULL);

导致:

GLib-GObject-WARNING **: invalid cast from `ClutterTexture' to `GtkClutterTexture'

Clutter-Gtk-CRITICAL **: gtk_clutter_texture_set_from_pixbuf: assertion `GTK_CLUTTER_IS_TEXTURE (texture)' failed

发生了什么,为什么会失败?以及如何调试它?

4

2 回答 2

1

GtkClutterTexture is a sub-class of ClutterTexture; this means that you can use GtkClutterTexture with every function accepting a ClutterTexture, but you cannot use a ClutterTexture with methods taking a GtkClutterTexture.

in the example, you create the texture using clutter_texture_new(), and then you pass that pointer to gtk_clutter_texture_set_from_pixbuf(). you either create a GtkClutterTexture, or you use the clutter_texture_set_from_rgb_data() function to set the image data from a GdkPixbuf, using something like:

  clutter_texture_set_from_rgb_data (CLUTTER_TEXTURE (texture),
                                     gdk_pixbuf_get_pixels (pixbuf),
                                     gdk_pixbuf_get_has_alpha (pixbuf),
                                     gdk_pixbuf_get_width (pixbuf),
                                     gdk_pixbuf_get_height (pixbuf),
                                     gdk_pixbuf_get_rowstride (pixbuf),
                                     gdk_pixbuf_get_has_alpha (pixbuf) ? 4 : 3,
                                     CLUTTER_TEXTURE_NONE,
                                     &gerror);

which is exactly what GtkClutterTexture.set_from_pixbuf() does.

于 2012-03-13T21:04:08.917 回答
0

您正在将未初始化的GtkClutterTexture *指针传递给仅包含垃圾的函数。您需要先创建一个GtkClutterTexture对象,gtk_clutter_texture_new()然后才能用 pixbuf 填充它。

编辑: 在您更新的示例中,您有一个clutter_texture_new(). 这与 不同gtk_clutter_texture_new(),因此将其转换为GTK_CLUTTER_TEXTURE()尝试将其转换为不是的类型,从而产生运行时警告。

于 2012-03-13T06:33:59.643 回答