gtk初学者应用程序无法使用断言失败

时间:2009-11-17 12:25:54

标签: c gtk

我在Gtk中完成了这个简单的应用程序,只是为了测试一下......我来自摇摆,所以重新定义绘制事件功能对我来说是正常的......无论如何似乎不起作用:

#include <gtk-2.0/gtk/gtk.h>
#include <gtk-2.0/gdk-pixbuf/gdk-pixbuf.h>
#include <stdlib.h>
#include<string.h>
#include<stdio.h>
#include <iostream>

GdkPixbuf *imm;

void destroy(GtkWidget *widget, gpointer data) {
    gtk_main_quit();
}

gboolean on_expose_event(GtkWidget *widget, GdkEventExpose *event,
        gpointer data) {
    gdk_draw_pixbuf((GdkDrawable*) widget, widget->style->white_gc, imm, 0, 0,
            0, 0, -1, -1, GDK_RGB_DITHER_NONE, 0, 0);
    return FALSE;
}

int main(int argc, char** argv) {

    char* filename = new char[1000];
    GError *error = NULL;
    GtkWidget *window;

    gtk_set_locale();
    gtk_init(&argc, &argv);

    if (argv[1] == NULL) {
        std::cout << "Err.";
        return -1;
    }

    strcpy(filename, argv[1]);

    imm = gdk_pixbuf_new_from_file(filename, &error);

    if (!imm) {
        std::cout << "err closing";
        return 0;
    }

    window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
    gtk_widget_set_size_request((GtkWidget*) window, 500, 350);
    g_signal_connect(G_OBJECT(window), "destroy", G_CALLBACK(destroy), NULL);
    g_signal_connect(window, "expose-event",
            G_CALLBACK(on_expose_event), NULL);

    gtk_container_set_border_width(GTK_CONTAINER (window), 10);
    gtk_widget_show(window);
    gtk_main();

    return 0;
}

..实际上在runtinme说(在线gdk_draw_pixbuf(....)):

(cConvolve:5011): Gdk-CRITICAL **: gdk_draw_pixbuf: assertion `GDK_IS_DRAWABLE (drawable)' failed

是因为pixbuf不好???或者是因为我不能这样画到窗口?

3 个答案:

答案 0 :(得分:5)

这是因为您将GtkWidget投放到GdkDrawable,而GtkWidget不会继承GdkDrawable。使用

gdk_draw_pixbuf(GDK_DRAWABLE(gtk_widget_get_window(widget)), blah blah...);

无论如何,在正常的GTK使用中,您不必在暴露处理程序中进行任何绘制。要显示图像,只需使用GtkImage小部件:

GtkImage *image = gtk_image_new_from_file(filename);
gtk_container_add(GTK_CONTAINER(window), image);

答案 1 :(得分:1)

小部件没有inherit from GdkDrawable,你需要从小部件的窗口中获取可绘制的内容。

答案 2 :(得分:0)

感谢。但我的目的是尝试在窗户上画画...无论如何,即使我不再断言,它也不起作用:

gboolean on_expose_event(GtkWidget *widget, GdkEventExpose *event,
        gpointer data) {
    int w, h;
    w = gdk_pixbuf_get_width(imm);
    h = gdk_pixbuf_get_height(imm);

    gdk_draw_pixbuf((GdkDrawable*) widget->window, widget->style->fg_gc[ GTK_STATE_NORMAL ], imm, 0, 0,
            0, 0, w, h, GDK_RGB_DITHER_NORMAL, 0, 0);


    return FALSE;
}