FLTK在按钮释放时从输入中获取价值 [英] FLTK Getting value from input on button release
问题描述
我已经完成了一些教程,当我按下按钮时可以打印一些内容,但是我不知道如何存储插入到输入小部件中的值,将用户购买到变量中供我使用.我是C ++和FLTK的新手,所以不确定是否可以使用Java扫描程序之类的简单工具.我假设您将使用类似 var = input-value();
的方法,但是我不知道如何在回调中使用它,因为它们仅采用某些参数.如:
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.
推荐答案
您正在考虑的级别太低.只需在更高的级别上使用它即可:回调是单击操作的结尾:而不是按下或释放.
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();
}
这篇关于FLTK在按钮释放时从输入中获取价值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!