Frage

I am trying to master the GObject Library. So I tried to make a simple Gtk+ Custom Widget by inheriting from GtkHBox. I can't figure out what the problem is or even where the problem is so I'll have to paste the entire code. Here is the code:

notetab.h

#ifndef NOTETAB_H
#define NOTETAB_H

G_BEGIN_DECLS

#define PRO_NOTE_TAB(obj) GTK_CHECK_CAST(obj, pro_note_tab_get_type (), ProNoteTab)
#define GTK_CPU_CLASS(klass) GTK_CHECK_CLASS_CAST(klass, pro_note_tab_get_type(), ProNoteTabClass)
#define GTK_IS_CPU(obj) GTK_CHECK_TYPE(obj, pro_note_tab_get_type())

typedef struct _ProNoteTab ProNoteTab;
typedef struct _ProNoteTabClass ProNoteTabClass;

struct _ProNoteTab
{
    GtkWidget hbox;
    GtkObject parent_instance;
    GtkLabel label;
    GtkButton cbtn;
};

struct _ProNoteTabClass
{
    GtkHBoxClass parent_class;
};

GtkType pro_note_tab_get_type(void);
GtkWidget* pro_note_tab_new(void);

G_END_DECLS

#endif

notetab.c

#include "common.h"
#include "notetab.h"

GtkType pro_note_tab_get_type()
{
    GtkType pro_note_tab_type = 0;

    if (!pro_note_tab_get_type)
    {
        static const GtkTypeInfo pro_note_tab_info =
        {
            "ProNoteTab",
            sizeof(ProNoteTab),
            sizeof(ProNoteTabClass),
            (GtkClassInitFunc) NULL,
            (GtkObjectInitFunc) NULL,
            NULL,
            NULL,
            (GtkClassInitFunc) NULL
        };

        pro_note_tab_type = gtk_type_unique(GTK_TYPE_WIDGET, &pro_note_tab_info);
    }

    return pro_note_tab_type;
}

GtkWidget* pro_note_tab_new(void)
{
    return GTK_WIDGET(gtk_type_new(pro_note_tab_get_type()));
}

Now the program compiles perfectly fine. But the error I get at runtime is:

GTK_CRITICAL**: IA__gtk_type_new : assertion GTK_TYPE_IS_OBJECT(type) failed GTK_CRITICAL**: IA__gtk_container_add : assertion GTK_IS_WIDGET(widget) failed

What am I doing wrong? Or even I what in the world is this error about?

War es hilfreich?

Lösung

According to the docs, gtk_type_unique is "deprecated and should not be used in newly-written code".

Use g_type_register_static instead. More so if you are trying to master GObject, not old Gtk+.

Anyway, I'd say that your error is due to some of the NULL function pointers you are setting, some are probably not optional, but this is poorly documented.

Andere Tipps

For one, the pro_note_tab_type variable in pro_note_tab_get_type() really looks like it should be static.

That must be the problem

 if (!pro_note_tab_get_type)
{
Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top