问题
I've done some tutorials and can get some things to print when I press a button but I cannot figure out how to store a value that is inserted into an input widget buy the user into a variable for me to use. I'm new to C++ and FLTK so I'm not sure if there's a simple thing like a Java Scanner to use. I'm assuming you would use something like var=input-value();
but I don't know how to use it within the callbacks since they only take certain parameters. Such as:
Fl_Button *butts[2];
static void Button_cb(Fl_Widget * w, void* data){
Fl_Button *b = (Fl_Button*)w;
fprintf(stderr, "Button '%s' was %s\n", b->label(), b->value() ? "Pushed" : "Released");
}
I can't just replace the print line for it to work. None of the tutorials I found and went through explained this.
回答1:
You're thinking at too low a level. Just use it at a slightly higher level: the callback is the end of the clicking operation: not the press or the release.
#include <FL/Fl.H>
#include <FL/Fl_Window.H>
#include <FL/Fl_Input.H>
#include <FL/Fl_Int_Input.H>
#include <FL/Fl_Button.H>
#include <FL/Fl_Color_Chooser.H>
struct Info
{
// The widgets
Fl_Input* instr;
Fl_Int_Input* inint;
// Saved values
char sval[40];
int ival;
};
// Callback for the done button
void done_cb(Fl_Widget* w, void* param)
{
Info* input = reinterpret_cast<Info*>(param);
// Get the values from the widgets
strcpy (input->sval, input->instr->value());
input->ival = atoi(input->inint->value());
// Print the values
printf("String value is %s\n", input->sval);
printf("Integer value is %d\n", input->ival);
}
int main(int argc, char **argv)
{
Info input;
// Setup the colours
Fl::args(argc, argv);
Fl::get_system_colors();
// Create the window
Fl_Window *window = new Fl_Window(200, 150);
int x = 50, y = 10, w = 100, h = 30;
input.instr = new Fl_Input(x, y, w, h, "Str");
input.instr->tooltip("String input");
y += 35;
input.inint = new Fl_Int_Input(x, y, w, h, "Int");
input.inint->tooltip("Integer input");
y += 35;
Fl_Button* done = new Fl_Button(x, y, 100, h, "Done");
done->callback(done_cb, &input);
window->end();
window->show(argc, argv);
return Fl::run();
}
来源:https://stackoverflow.com/questions/28441540/fltk-getting-value-from-input-on-button-release