GUI code crashes but compiles

Hi Guys

I am new to pgramming for C++ and trying to create a GUI where a box color changes when a button is pressed (it has some radio buttons as well which works fine). The code compiles fine but crashed when I run it. I think the issue is in the "mystart" function /callback because when I comment it out the code runs. I am stuck because the "my exit" is written the same and it works fine

I am using FLTK and it is running on a linux system.

any help would be greatly appreciated. The code is below


[#include <FL/Fl.H>
#include <FL/Fl_Window.H>
#include <FL/Fl_Box.H>
#include <FL/Fl_Browser.H>
#include <FL/Fl_Button.H>
#include <FL/Fl_Widget.H>
#include <FL/names.h>
#include <stdio.h>
#include <getopt.h>
#include <stdlib.h>
#include <string.h>
#include <FL/Fl_Text_Editor.H>
#include <FL/Fl_Timer.H>
#include <FL/Fl_Group.H>
#include <FL/Fl_Round_Button.H>
#include <FL/Fl_Menu_Bar.H>


//Vanessa Wood 3 July 2012
//----------------------


Fl_Box* box1;
int radio;

static void cb_radio(Fl_Widget *b, void *d)
{
char msg[64];
radio=(int)d;

}

// exit function

void myexit(Fl_Widget*o) // borrowed from GetProgCounts
{
exit(0);
}

// change color function

void mystart(Fl_Widget*b)
{

if (box1->color() == FL_RED)
{
box1->color(FL_GREEN); //toggle
}
else
{
box1->color(FL_RED);//toggle
}
}



// Main
int main()
{
Fl_Window win(240, 300," logger");//240, 300
win.begin();

// change button color
//Create a button - x , y , width, height, label

Fl_Box* box1 = new Fl_Box(FL_FLAT_BOX,25,15,20,20,"");
box1->color(FL_RED);


Fl_Box* led2 = new Fl_Box(FL_FLAT_BOX,25,45,20,20,"");//35,15,20,20
led2->color(FL_GREEN);


// radio buttons in window

Fl_Group* rb_group = new Fl_Group(20, 90, 200, 105);//40
rb_group->box(FL_UP_FRAME);

{
Fl_Round_Button* rb1 = new Fl_Round_Button(25, 95, 70, 30, "&HS GM LAN");
rb1->type(102);
rb1->down_box(FL_ROUND_DOWN_BOX);

}

{
Fl_Round_Button* rb2 = new Fl_Round_Button(25, 125, 70, 30, "&LS GM LAN");
rb2->type(102);
rb2->down_box(FL_ROUND_DOWN_BOX);
;
}

{
Fl_Round_Button* rb3 = new Fl_Round_Button(25, 155, 70, 30, "& Both");
rb3->type(102);
rb3->setonly();
}
rb_group->end();

// add start and exit button

Fl_Button *exitButton;
Fl_Button *startButton;
startButton->callback(mystart); // button to change color of box

exitButton = new Fl_Button (120, 250, 100, 40, "Exit");
startButton = new Fl_Button (20, 250, 100, 40, "Start");
exitButton->callback(myexit);

win.end();
win.show();
return Fl::run();
}


code][/code]
Last edited on
scope.
You've got a global Fl_Box* box1; that is accessed in the callback.
And a local Fl_Box* box1; that is created and filled inside main()

Those two are different, so the global one remains invalid.
Hi
Thank you so much for your input, your suggestion was spot on.

I changed the main so it was not created in main anymore and the code no longer crashes which is great.

However the mystart function is not being actioned when I press the start button.

i.e. box 1 stays red and does not change to green when the start button is pressed

Any idea's I have tried several changes to the function and with not much luck. I also moved the startbutton callback to the end of the main. I have attached the changed code below

Again I really appreciate your help.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
[#include <FL/Fl.H>
#include <FL/Fl_Window.H>
#include <FL/Fl_Box.H>
#include <FL/Fl_Browser.H>
#include <FL/Fl_Button.H>
#include <FL/Fl_Widget.H>
#include <FL/names.h>
#include <stdio.h>
#include <getopt.h>
#include <stdlib.h>
#include <string.h>
#include <FL/Fl_Text_Editor.H>
#include <FL/Fl_Timer.H>
#include <FL/Fl_Group.H>
#include <FL/Fl_Round_Button.H>
#include <FL/Fl_Menu_Bar.H>


//Vanessa Wood 3 July 2012
//----------------------


Fl_Box* box1;
int radio;

static void cb_radio(Fl_Widget *b, void *d)
{
char msg[64];
radio=(int)d;

}

// exit function

void myexit(Fl_Widget*o) // borrowed from GetProgCounts
{
exit(0);
} 

// change color function

void mystart(Fl_Widget*b) 
{

if (box1->color() == FL_RED)
{
box1->color(FL_GREEN); //toggle
}
else 
{
box1->color(FL_RED);//toggle
}
}



// Main
int main()
{
Fl_Window win(240, 300," logger");//240, 300
win.begin();

// change button color
//Create a button - x , y , width, height, label

box1 = new Fl_Box(FL_FLAT_BOX,25,15,20,20,"");
box1->color(FL_RED);


Fl_Box* led2 = new Fl_Box(FL_FLAT_BOX,25,45,20,20,"");//35,15,20,20
led2->color(FL_GREEN);


// radio buttons in window

Fl_Group* rb_group = new Fl_Group(20, 90, 200, 105);//40
rb_group->box(FL_UP_FRAME);

{ 
Fl_Round_Button* rb1 = new Fl_Round_Button(25, 95, 70, 30, "&HS GM LAN");
rb1->type(102);
rb1->down_box(FL_ROUND_DOWN_BOX);

} 

{ 
Fl_Round_Button* rb2 = new Fl_Round_Button(25, 125, 70, 30, "&LS GM LAN");
rb2->type(102);
rb2->down_box(FL_ROUND_DOWN_BOX);
;
} 

{ 
Fl_Round_Button* rb3 = new Fl_Round_Button(25, 155, 70, 30, "& Both");
rb3->type(102);
rb3->setonly();
} 
rb_group->end();

// add start and exit button

Fl_Button *exitButton;
Fl_Button *startButton;

exitButton = new Fl_Button (120, 250, 100, 40, "Exit"); 
startButton = new Fl_Button (20, 250, 100, 40, "Start"); 

exitButton->callback(myexit);
startButton->callback(mystart); // button to change color of box


win.end();
win.show();
return Fl::run();
} 


I think that you need to define when the callback is trigger
http://www.fltk.org/doc-1.1/Fl_Button.html#Fl_Button.when

Also note that you could define the callback to take an object, so you don't need it to be global
Hi ne555
Thank you again for you help.
After playing with when for the callback I relised that the code was actually working and it was just the box color not refreshing.
After adding a redraw for box1 in the mystart function it is now updating the color change.
Very exciting

I have attached the now working code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
#include <FL/Fl.H>
#include <FL/Fl_Window.H>
#include <FL/Fl_Box.H>
#include <FL/Fl_Browser.H>
#include <FL/Fl_Button.H>
#include <FL/Fl_Widget.H>
#include <FL/names.h>
#include <stdio.h>
#include <getopt.h>
#include <stdlib.h>
#include <string.h>
#include <FL/Fl_Text_Editor.H>
#include <FL/Fl_Timer.H>
#include <FL/Fl_Group.H>
#include <FL/Fl_Round_Button.H>
#include <FL/Fl_Menu_Bar.H>



//---- State machines-----
// Parts of this code has been borrowed from code that has already written.  I would like to agknowlegde these people and 
//Selects of code they are as follows

//Vanessa Wood 3 July 2012
//----------------------


Fl_Box* box1;

int radio;
static void cb_radio(Fl_Widget *b, void *d)
{
   char msg[64];
   radio=(int)d;
   
}
// exit function
 void myexit(Fl_Widget*o) // borrowed from GetProgCounts
{
	exit(0);
} 

// change color function

void mystart(Fl_Widget*, void*)

{

 if (box1->color() == FL_RED)
 {
  box1->color(FL_GREEN); //toggle
 }
 else 
 {
  box1->color(FL_RED);//toggle
 }
  
  box1->redraw();
}



// Main
int main()
{
      Fl_Window win(240, 300," logger");//240, 300
      win.begin();

      // change button color
        box1 = new Fl_Box(FL_FLAT_BOX,25,15,20,20,"");
      box1->color(FL_RED);
    

      Fl_Box* led2 = new Fl_Box(FL_FLAT_BOX,25,45,20,20,"");//35,15,20,20
      led2->color(FL_GREEN);
            
      // radio buttons in window
      
      Fl_Group* rb_group = new Fl_Group(20, 90, 200, 105);//40
      rb_group->box(FL_UP_FRAME);

      { 
        Fl_Round_Button* rb1 = new Fl_Round_Button(25, 95, 70, 30, "&HS GM LAN");
        rb1->type(102);
        rb1->down_box(FL_ROUND_DOWN_BOX);
      
      } 
      
      { 
	    Fl_Round_Button* rb2 = new Fl_Round_Button(25, 125, 70, 30, "&LS GM LAN");
        rb2->type(102);
        rb2->down_box(FL_ROUND_DOWN_BOX);
        ;
      } 

      {     
        Fl_Round_Button* rb3 = new Fl_Round_Button(25, 155, 70, 30, "& Both");
        rb3->type(102);
        rb3->setonly();
      } 
rb_group->end();

// add start and exit button
         
         
         Fl_Button *startButton;
         startButton = new Fl_Button (20, 250, 100, 40, "Start");
         startButton->callback(mystart);
         
         Fl_Button *exitButton;
         exitButton = new Fl_Button (120, 250, 100, 40, "Exit");       
         exitButton->callback(myexit);
                                        


win.end();
win.show();
return Fl::run();
} 
Topic archived. No new replies allowed.