I try to read value from Fl_Input and set up Fl_Slider by using code below but something is not working! I'm not sure about the operation
Thanks for help!
FL_Value_Input* vinput;
void set_slider(FL_Widget* w, void* v)
{
Fl_Slider* slider = (Fl_Slider*)w;
slider->value(vinput->value());
}
int main(){
Fl_Window *window = new Fl_Window(100, 100, 600, 400, "callback");
window->begin();
Fl_Value_Input *vinput = new Fl_Value_Input(40, 40, 40, 40);
vinput->textsize(30);
Fl_Button *button = new Fl_Button(160, 40, 120, 40, "Set slider");
button->labelsize(15);
Fl_Slider *slid = new Fl_Slider(40, 200, 400, 30);
slid->slider(FL_UP_FRAME);
((Fl_Widget*)slid)->type(FL_HOR_NICE_SLIDER);
slid->bounds(10, -10);
slid->slider_size(10);
slid->range(-10, 10);
slid->step(1);
slid->value(0);
button->callback(set_slider);
window->end();
window->show();
return Fl::run();
}
You are setting your slider in the button callback - the widget is the button: not the slider. Also the vinput you are referring to is a global, not the local in main.
To fix, pass a structure with both the input and the slider to the callback.
// Create a structure for input and slider
struct Info
{
FL_Value_Input* vinput;
FL_Slider* slider;
}
// Callback
void set_slider(FL_Widget* w, void* v)
{
Info* info = (Info*) v;
info->slider->value(info->vinput->value());
}
....
// Declare Info in your main code
Info info;
...
info->vinput = new Fl_value_input...
...
info->slider = new Fl_slider...
...
button->callback(set_slider, (void*)&info);
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.