如何在单击按钮FLTK的确切时刻获得小部件的值
How can I get the value of a widget the exact moment a button is clicked FLTK
目标是能够在我单击按钮时获取小部件的值,例如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
,并将后者传递给按钮的回调。
请注意,我编写的代码不是很优雅,也不是最有效的:它只是向您展示我解决您问题的想法(如果我理解正确的话(。
相关文章:
- 比较并显示使用最小值(a,b)和最大值(a、b)升序排列的4个数字
- 最小硬币更换问题(自上而下方法)
- Qt VTK交互风格的信号到小部件
- 芬威克树(BIT).找到具有给定累积频率的最小索引,单位为 O(logN)
- 如何在Qt窗口小部件中使用QStringView(或QStringRef)
- 使用递归的数组的最小值.这是怎么回事
- 为什么在浮点中从大到小会引入更多的误差
- 找到两对数字,使它们的乘积的绝对差最小化
- 如何使用boost::具有嵌套结构和最小代码更改的序列化
- 最大和最小 1 C++函数
- 如何创建一个函数来计算并返回平均值、最大值和最小值
- 我可以做些什么来消除或最小化这种将提供相同功能和行为的代码重复
- 如何将部分流作为参数传递
- 如何在QT中的自定义视频小工具t上绘制矩形
- 在二维数组中查找最小值和最大值?
- 查找矩阵C++中每一列和每一行的最小和最大元素
- 如何找到大于整数的最小数字
- 小字符串优化(调试与发布模式)
- 如何在单击按钮FLTK的确切时刻获得小部件的值
- C++ 创建小部件数组