如何在单击按钮FLTK的确切时刻获得小部件的值

How can I get the value of a widget the exact moment a button is clicked FLTK

本文关键字:时刻 小部 单击 按钮 FLTK      更新时间:2023-10-16

目标是能够在我单击按钮时获取小部件的值,例如FL_Input。

我可以做到这一点的一种方法是使用事件和类。如

#include <FL/Fl.H>
#include <FL/Fl_Window.H>
#include <FL/Fl_Button.H>
#include <iostream>
using namespace std;
class MyButton : public Fl_Button
{
static int count;
public:
MyButton(int x, int y, int w, int h, const char*l = 0)
:Fl_Button(x, y, w, h, l) {}
int handle(int e)
{
int ret = Fl_Button::handle(e);
//test - - - cout << endl << count++ << " ******** button " << label() << " receives ";

switch (e)
{
case FL_PUSH:
trigger..
break;

}
return(ret);
}
};
int MyButton::count = 0;
void but_a_cb(Fl_Widget* w, void* v) {
cout << endl << "Button A callback!" << endl;
}

int main()
{
Fl_Window win(120, 150);
win.begin();
MyButton but_a(10, 10, 100, 25, "A");
but_a.shortcut('a');
but_a.callback(but_a_cb);

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

但是,这对我来说不是执行此操作的最佳方法,因为我想获取多个输入并将它们放入一个向量中并在此中使用它们。为此,我正在考虑一个将返回输入值的输入回调,我会使用"Fl_Input->when(FL_CHANGED(">

最好我能够通过使用 main 函数而不是按钮中的代码来获取值并在单击按钮时将它们放入向量中,因为这需要我重新设计我的程序来实现这一点。

仅在 main 中声明向量不起作用,因为它在表单加载时取值。

总而言之,我想知道如何创建类似 if 语句的东西,该语句在单击按钮时触发,以便在单击按钮时获取 fl_inputs 的值。T

注意 我将使用数据库实现这一点,以便我需要它与 CLR 一起使用

我想出的额外代码

void done_cb(Fl_Widget* w, void* param)
{
Fl_Input* i = (Fl_Input*)param;
string inputstring = i->value();
cout << inputstring;
// What i tried for the vectors
//vector<Fl_Input*> v = *reinterpret_cast<vector<Fl_Input*> *>(param);
//string inputstring = v[0]->value();
//cout << inputstring;

}
void signupScreen (void) {
Fl::event_dispatch(myHandler);

Fl_Window *window = new Fl_Window(200, 150);
window->begin();
int x = 50, y = 10, w = 100, h = 30;
y += 35;

y += 35;
Fl_Input *input2 = new Fl_Input(x, 10, w, h, "str");

Fl_Button* done = new Fl_Button(x, y, 100, h, "Done");
done->when(FL_WHEN_CHANGED);

string strinput;
//strinput = input2->value();

//input2->when(FL_WHEN_CHANGED);

//std::vector<Fl_Input*> v = { input2 };
done->callback(done_cb, input2);

cout << strinput;

window->end();

window->show();

}

您可以定义一个派生自Fl_Input的新类,该类具有类型为std::vector<std::string>的成员,您可以在其中存储输入字段中写入的文本。

#ifndef __MYINPUT_H
#define __MYINPUT_H
#include <FL/Fl_Window.H>
#include <FL/Fl.H>
#include <FL/Fl_Input.H>
#include <iostream> 
#include <vector>

class myinput: public Fl_Input
{    
public:
/* 
Instead of setting it public, you may create get and set methods.
*/
std::vector<std::string> _storing;
// Constructor 
myinput(int x, int y, int h, int l, std::vector<std::string> S, const char* ll = 0);
~myinput(){};

};
#endif

.ccp文件:

#include <myinput.hpp>
myinput::myinput(int x, int y, int h, int l,std::vector<std::string> S,const char* ll):Fl_Input(x, y, h,l,ll)
{
_storing = S;
}

然后,您可以按如下所示使用它:

#include <FL/Fl.H>
#include <FL/Fl_Window.H>
#include <myinput.hpp>
#include <FL/Fl_Button.H>
void add_cb(Fl_Widget* w, void* p){
myinput* i = (myinput*)p;
std::string inputstring = i->value();
i->_storing.push_back(inputstring);
// Just to show that it is working
std::cout<< "Updated string:" << std::endl;
for (int j=0; j<i->_storing.size(); j++ )
std::cout << i->_storing[j] << std::endl;
}

int main(int argc, char **argv) {
Fl_Window *G_win = 0;
G_win = new Fl_Window(300,300,"Test");
myinput* A;
// Where you may store the strings
std::vector<std::string> mytext;
A = new myinput(100,50,100,20, mytext,"Insert text");
Fl_Button* add;
add = new Fl_Button(250,50,25,25,"+");
add->callback(add_cb,A);

G_win->end();
G_win->show(argc, argv);

return Fl::run();
}

如果要存储来自多个输入字段的字符串,则可以在Fl_Group中收集Fl_input,并将后者传递给按钮的回调。

请注意,我编写的代码不是很优雅,也不是最有效的:它只是向您展示我解决您问题的想法(如果我理解正确的话(。