gtk_adjustment_get_value分段错误

时间:2014-01-10 02:17:17

标签: c gtk cairo

我正在尝试使用GTK +和Cairo在C中编写一些简单的东西,但是我在使用gtk_adjustment_get_value函数时遇到了一些麻烦。

这是我的代码:

#include <cairo.h>
#include <gtk/gtk.h>
#include <math.h>
#include <stdlib.h>
#include <gtk/gtkhscale.h>
#include <math.h>

typedef struct
{
  GtkAdjustment *adj[2];    //Adjustments para as escalas
  GtkWidget *scale[2];      //Escalas
} ProgInfo;

gboolean
on_expose_event (GtkWidget *widget, 
             GdkEventExpose *event, 
         gpointer data)
{
  cairo_t *cr;
  double pos1x, pos1y;
  static gdouble pos2x = 450., pos2y = 290.; //Coordenadas Espelho
  static gdouble pos3x = 450., pos3y = 250.;
  ProgInfo *pinfo;

  cr = gdk_cairo_create(widget->window);

  pinfo = (ProgInfo *) data;

  pos1x = gtk_adjustment_get_value (pinfo->adj[0]);

  //Objecto

  cairo_set_source_rgb (cr, 0, 0, 0);
  cairo_set_line_width (cr, 1.0);
  cairo_rectangle (cr, (double) pos1x, (double) pos1y, 20, 80);
  cairo_stroke_preserve (cr);
  cairo_set_source_rgb (cr, 1, 1, 1);
  cairo_fill (cr);

  cairo_set_source_rgb (cr, 0, 0, 0);
  cairo_set_line_width (cr, 1.0);
  cairo_rectangle (cr, (gint) pos3x, (gint) pos3y, 5, 80);
  cairo_stroke_preserve (cr);
  cairo_set_source_rgb (cr, 1, 1, 1);
  cairo_fill (cr);

  cairo_stroke(cr);
  cairo_destroy(cr);

  return FALSE;
}


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

  GtkWidget *window, *vbox, *vbox1, *frame, *frame1, *frame2, *label, *label1, *label2, *hbox, *hbox1, *hscale;
  char string[40] = "Posição Objecto", string1[30] = "Posição Espelho";
  ProgInfo *pinfo;
  gdouble pos1x;
  pinfo = (ProgInfo *) calloc (1, sizeof (ProgInfo));

  gtk_init(&argc, &argv);

  window = gtk_window_new(GTK_WINDOW_TOPLEVEL);

  g_signal_connect (window, "expose-event", G_CALLBACK(on_expose_event), NULL);
  g_signal_connect (window, "destroy", G_CALLBACK(gtk_main_quit), NULL);

  gtk_window_set_position (GTK_WINDOW(window), GTK_WIN_POS_CENTER);
  gtk_window_set_title (GTK_WINDOW(window), "Projecto Óptica");
  gtk_window_set_default_size (GTK_WINDOW(window), 800, 600); 
  gtk_widget_set_app_paintable (window, TRUE);

  vbox = gtk_vbox_new (FALSE, 0);
  gtk_container_add (GTK_CONTAINER (window), vbox);

  hbox1 = gtk_hbox_new (FALSE, 0);
  gtk_container_add (GTK_CONTAINER (vbox), hbox1);

  vbox1 = gtk_vbox_new (FALSE, 0);
  gtk_box_pack_end (GTK_BOX (hbox1), vbox1, FALSE, FALSE, 20);

  //Posição Objecto
  label2 = gtk_label_new (string);
  gtk_box_pack_start (GTK_BOX (vbox1), label2, FALSE, FALSE, 20);

  pinfo->adj[0] = (GtkAdjustment *) gtk_adjustment_new (0.7, 0.0, 1.0, 0.001, 1.0, 0.0);
  //Os parâmetros são, por ordem: valor inicial, valor mínimo e valor máximo
  pinfo->scale[0] = gtk_hscale_new (GTK_ADJUSTMENT (pinfo->adj[0]));
  gtk_box_pack_start (GTK_BOX (vbox1), pinfo->scale[0], FALSE, TRUE, 5);

  //Posição Espelho
  label1 = gtk_label_new (string1);
  gtk_box_pack_start (GTK_BOX (vbox1), label1, FALSE, FALSE, 20);

  pinfo->adj[1] = (GtkAdjustment *)gtk_adjustment_new (0.0, 0.0, 101.0, 0.1, 1.0, 1.0);
  pinfo->scale[1] = gtk_hscale_new (GTK_ADJUSTMENT (pinfo->adj[1]));
  gtk_box_pack_start (GTK_BOX (vbox1), pinfo->scale[1], FALSE, FALSE, 0);

  gtk_widget_show_all(window);
  gtk_main();

  return 0;
}

我用hscale小部件创建了几个滑块,它们在窗口中显示得很好,我可以移动它们,一切都没问题。但我想让我的变量pos1x在我移动滑块时改变。我尝试使用gtk_adjustment_get_value函数来完成它,但是我遇到了分段错误。我注意到如果我注释掉这一行:

  pos1x = gtk_adjustment_get_value (pinfo->adj[0]);

分段错误不再发生,但应用程序也没有按我的意愿行事。我意识到这可能非常简单,而我的怀疑来自对GTK +如何工作的稀缺理解,但我一直在尝试过去几个小时,我无法弄清楚出了什么问题。我很感激您提供的任何帮助。

哦,很抱歉在这里放了如此大的代码,但我不确定你需要哪些部分。

1 个答案:

答案 0 :(得分:4)

此处连接到expose-event,userdata指针设置为NULL:

g_signal_connect (window, "expose-event", G_CALLBACK(on_expose_event), NULL);

但在处理程序中,您希望它是指向ProgInfo的指针。您应该将NULL替换为pinfo