你知道如何在GTK 3中创建自定义小部件吗?我试图在C中继承GtkDrawingArea几个小时。 Gnome.org仅提供了关于如何子类化G_OBJECT的简明教程。我的问题是G_Object
/ GTK
无法在使用StrokerNodalContainer
进行投放时将我的自定义GtkWidget
视为GTK_WIDGET
的子类,即使我的定义很难{{1}包含这样一行:
struct
它说:
GtkDrawingArea parent_instance;
如果您怀疑其他可能有问题,请输入完整的代码。它很小,所以我没有看到任何外部代码混乱的原因。
冲击器-nodalcontainer.h
invalid cast from 'StrokerNodalContainer' to 'GtkWidget'
冲击器-nodalcontainer.c
#ifndef __STROKER_NODALCONTAINER_H__
#define __STROKER_NODALCONTAINER_H__
#ifndef NO_INCLUDE_WITHIN_HEADERS
#include <gtk/gtk.h>
#endif
#define STROKER_TYPE_NODAL_CONTAINER (stroker_nodal_container_get_type ())
#define STROKER_NODAL_CONTAINER(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), STROKER_TYPE_NODAL_CONTAINER, StrokerNodalContainer))
#define STROKER_NODAL_CONTAINER_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST ((klass), STROKER_TYPE_NODAL_CONTAINER, StrokerNodalContainerClass))
#define STROKER_IS_NODAL_CONTAINER(obj) (G_TYPE_CHECK_INSTANCE_TYPE ((obj), STROKER_TYPE_NODAL_CONTAINER))
#define STROKER_IS_NODAL_CONTAINER_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), STROKER_TYPE_NODAL_CONTAINER))
#define STROKER_NODAL_CONTAINER_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS ((obj), STROKER_TYPE_NODAL_CONTAINER, StrokerNodalContainerClass))
typedef struct _StrokerNodalContainer StrokerNodalContainer;
typedef struct _StrokerNodalContainerClass StrokerNodalContainerClass;
struct _StrokerNodalContainer
{
GtkDrawingArea parent_instance;
};
struct _StrokerNodalContainerClass
{
GtkDrawingAreaClass parent_class;
};
GType stroker_nodal_container_get_type(void);
//StrokerNodalContainer* stroker_nodalcontainer_new(void);
#endif /* __STROKER_NODALCONTAINER_H__ */
的main.c
#include <gtk/gtk.h>
#include "stroker-nodalcontainer.h"
G_DEFINE_TYPE( StrokerNodalContainer, stroker_nodal_container, G_TYPE_OBJECT )
static void stroker_nodal_container_class_init( StrokerNodalContainerClass* klass )
{}
static void stroker_nodal_container_init( StrokerNodalContainer* self )
{
GdkRGBA c;
GtkWidget *widget;
gdk_rgba_parse(&c, "blue");
widget = GTK_WIDGET(self);
gtk_widget_override_background_color( widget, GTK_STATE_FLAG_NORMAL, &c );
}
感谢您的帮助!
答案 0 :(得分:4)
错误信息可能是因为:
G_DEFINE_TYPE( StrokerNodalContainer, stroker_nodal_container, G_TYPE_OBJECT )
如果你查看documentation for G_DEFINE_TYPE(),你会看到第三个参数应该是父类型:你可能想要GTK_TYPE_DRAWING_AREA
。