c-GTK得到多个距离值的分割误差



所以,我试图在C.中创建一个带有gtk的rgb选择器

我按照文档学习如何放置glade项目并将其与gtk一起使用,所以我在glade中制作了一个UI,但我在gtk部分遇到了一些问题。我需要访问更改值信号中的多个滑块,因为我想获得它们的值来打印它们,所以我创建了一个结构来存放它们,并在信号中发送这个结构。但我的问题是,当我试图获得他在结构中股票的区间值时,我出现了分段错误,我不知道为什么。我尝试不使用结构,可以正确地获得滑块值,但它不适用于结构。我该怎么修?

问我你是否也需要glade文件。

//Compil : gcc -rdynamic -o ColorPicker colorPicker.c `pkg-config --cflags --libs gtk+-3.0`
#include <gtk/gtk.h>
#include <glib/gstdio.h>
#include <gdk/gdk.h>
typedef struct
{
GObject *redSlider;
GObject *blueSlider;
GObject *greenSlider;
GObject *label;
}Data;

static void changeLabel (GtkWidget *range, Data *data)
{
gdouble red = gtk_range_get_value(GTK_RANGE(data->redSlider));
g_print("%.0lfn",red);
//g_print("R : %d, G : %d, B : %d",red,green,blue);
}

static void activate(GtkApplication *app, gpointer user_data)
{
Data *data;
data = malloc(sizeof(*data));
GtkBuilder *builder = gtk_builder_new();
gtk_builder_add_from_file(builder, "colorPicker.glade",NULL);
GObject *window = gtk_builder_get_object (builder, "window");
gtk_window_set_application (GTK_WINDOW (window), app);

data->redSlider = gtk_builder_get_object (builder, "redSlider");
g_signal_connect (data->redSlider, "change-value", G_CALLBACK (changeLabel), (gpointer) data);
data->blueSlider = gtk_builder_get_object (builder, "blueSlider");
g_signal_connect (data->blueSlider, "change-value", G_CALLBACK (changeLabel), (gpointer) data);
data->greenSlider = gtk_builder_get_object (builder, "greenSlider");
g_signal_connect (data->greenSlider, "change-value", G_CALLBACK (changeLabel), (gpointer) data);
data->label = gtk_builder_get_object (builder, "cpChooseLabel");
/*GObject *colorBtn = gtk_builder_get_object (builder, "colorBtn");
g_signal_connect (colorBtn, "clicked", G_CALLBACK (getColor), (gpointer) data);*/

gtk_widget_show (GTK_WIDGET (window));
/* We do not need the builder any more */
g_object_unref (builder);
free(data);
}
int main(int argc, char **argv)
{
#ifdef GTK_SRCDIR
g_chdir(GTK_SRCDIR);
#endif
GtkApplication *app = gtk_application_new("com.github.XXXXXX.rgbSelector", G_APPLICATION_FLAGS_NONE);
g_signal_connect(app, "activate", G_CALLBACK(activate), NULL);
int status = g_application_run(G_APPLICATION(app), argc, argv);
g_object_unref(app);
return status;
}

通过在activate结束时使用free来结束结构的使用寿命。稍后调用回调changeLabel时,它会尝试访问不再存在的结构。如果你不在乎四个指针占用的空间无法释放,那么你可以丢弃free(data);;另一种可能性是简单地使用静态结构并传递其地址。

相关内容

  • 没有找到相关文章

最新更新