我试图在C中使用GTK + 3编写复合小部件。
复合小部件集属性和get属性正在使用alsa_device_item **,它是_alsa_device_items的类型结构。
我的包含文件就是这个;
#ifndef AUDIO_DEVICE_DIALOG_WIDGET_H
#define AUDIO_DEVICE_DIALOG_WIDGET_H
#ifdef __cplusplus
extern "C" {
#endif
#include <gtk-3.0/gtk/gtk.h>
#define AUDIO_DEVICE_DIALOG_WIDGET_TYPE (audio_device_dialog_widget_get_type())
#define AUDIO_DEVICE_DIALOG_WIDGET(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), AUDIO_DEVICE_DIALOG_WIDGET_TYPE, AudioDeviceDialogWidget))
GType audio_device_dialog_widget_get_type (void);
typedef struct _audio_device_dialog_widget AudioDeviceDialogWidget;
typedef struct _audio_device_dialog_widget_class AudioDeviceDialogWidgetClass;
struct _audio_device_item {
char *device_name;
char *device_desc;
gboolean is_available;
};
typedef struct _audio_device_item audio_device_item;
AudioDeviceDialogWidget *audio_device_dialog_widget_new (audio_device_item **device_items);
#ifdef __cplusplus
}
#endif
#endif /* AUDIO_DEVICE_DIALOG_WIDGET_H */
正如您在include文件中看到的那样,我定义了alsa_device_item的struct和typedef。
c文件是这样的:
#include <gtk-3.0/gtk/gtk.h>
#include <string.h>
#include "audio_device_dialog_widget.h"
#include "audio_dev_box_widget.h"
typedef struct audio_device_dialog_widget_private AudioDeviceDialogWidgetPrivate;
struct _audio_device_dialog_widget
{
GtkDialog parent;
AudioDeviceDialogWidgetPrivate *priv;
};
struct _audio_device_dialog_widget_class
{
GtkDialogClass parent_class;
};
struct audio_device_dialog_widget_private
{
GtkWidget *save_button, *cancel_button, *audio_dialog_box;
};
G_DEFINE_TYPE_WITH_PRIVATE(AudioDeviceDialogWidget,audio_device_dialog_widget,GTK_TYPE_DIALOG);
enum {
PROP_0,
PROP_DEVICE_NAMES
};
static void audio_device_dialog_widget_init(AudioDeviceDialogWidget *widget) {
widget->priv = G_TYPE_INSTANCE_GET_PRIVATE(widget, AUDIO_DEVICE_DIALOG_WIDGET_TYPE, AudioDeviceDialogWidgetPrivate);
gtk_widget_init_template(GTK_WIDGET(widget));
}
AudioDeviceDialogWidget *audio_device_dialog_widget_new(audio_device_item **device_items) {
return g_object_new(AUDIO_DEVICE_DIALOG_WIDGET_TYPE,"devices_list",device_items,NULL);
}
static void audio_device_dialog_widget_set_devices(AudioDeviceDialogWidget *widget,const audio_device_item **device_items) {
g_return_if_fail(AUDIO_DEVICE_DIALOG_WIDGET(widget));
int i=0;
while (device_items[i] != NULL) {
AudioDevBoxWidget *audio_dev_box_widget = audio_dev_box_widget_new(device_items[i]->device_name, device_items[i]->device_desc, device_items[i]->is_available);
gtk_box_pack_start(GTK_BOX(widget->priv->audio_dialog_box),GTK_WIDGET(audio_dev_box_widget),FALSE, FALSE, 0);
i++;
}
}
const audio_device_item **audio_device_dialog_widget_get_devices(AudioDeviceDialogWidget *widget) {
g_return_val_if_fail (AUDIO_DEVICE_DIALOG_WIDGET (widget), NULL);
const audio_device_item **devices = NULL;
return devices;
}
static void
audio_device_dialog_widget_set_property (GObject *object,
guint prop_id,
const GValue *value,
GParamSpec *pspec)
{
AudioDeviceDialogWidget *widget = AUDIO_DEVICE_DIALOG_WIDGET (object);
switch (prop_id)
{
case PROP_DEVICE_NAMES:
audio_device_dialog_widget_set_devices(widget, g_value_get_pointer(value));
break;
default:
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
break;
}
}
static void
audio_device_dialog_widget_get_property (GObject *object,
guint prop_id,
GValue *value,
GParamSpec *pspec)
{^
AudioDeviceDialogWidget *widget = AUDIO_DEVICE_DIALOG_WIDGET (object);
switch (prop_id)
{
case PROP_DEVICE_NAMES:
g_value_set_pointer (value, audio_device_dialog_widget_get_devices(widget));
break;
default:
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
break;
}
}
static void audio_device_dialog_widget_class_init(AudioDeviceDialogWidgetClass *class) {
GtkWidgetClass *widget_class = GTK_WIDGET_CLASS(class);
GObjectClass *gobject_class = G_OBJECT_CLASS(class);
gobject_class->set_property=audio_device_dialog_widget_set_property;
gobject_class->get_property=audio_device_dialog_widget_get_property;
GParamSpec *obj_properties =
g_param_spec_string("devices_list","Devices List","Devices List",NULL,G_PARAM_READWRITE);
g_object_class_install_property(gobject_class, PROP_DEVICE_NAMES,obj_properties);
gtk_widget_class_set_template_from_resource (widget_class,
"/com/tuxin/tuxmusicstudio/audio_device_dialog.glade");
gtk_widget_class_bind_template_child_private(widget_class, AudioDeviceDialogWidget, save_button);
gtk_widget_class_bind_template_child_private(widget_class, AudioDeviceDialogWidget, cancel_button);
gtk_widget_class_bind_template_child_private(widget_class, AudioDeviceDialogWidget, audio_dialog_box);
}
当我运行我的应用程序时,我得到以下关键信息:
(tuxmusicstudio:15946): GLib-GObject-CRITICAL **: g_value_get_pointer: assertion 'G_VALUE_HOLDS_POINTER (value)' failed
因为你可以看到g_value_get_pointer不接受我的typedef结构指针。
任何想法?^
答案 0 :(得分:0)
您需要确定以下几点:
GValue
- 指针不是NULL
get
和 set
G_VALUE_HOLDS_POINTER(value)
为TRUE
G_VALUE_TYPE_NAME (value)
说的是什么(它实际上是什么类型)