fl_输入为字符串
FL_Input into a string
你好,我是新来的FLTK,现在我有一个非常简单的程序,如果你运行程序,点击创建然后机器人部分,你会看到几个选项选择一个。一个新窗口将打开几个FL_Input框和一个输入按钮,我想做一个回调,以便当用户按下输入FL_Input框中输入的文本保存为字符串,例如有人在FL_Input中输入Space head,然后该值被分配给字符串。我主要不需要帮助回调只是设置一个字符串等于FL_Input。非常感谢您的帮助
#include <iostream>
#include <sstream>
#include <stdio.h>
#include <FL/Fl.H>
#include <FL/Fl_Text_Display.H>
#include <FL/fl_draw.H>
#include <FL/Fl_Double_Window.H>
#include <FL/Fl_Window.H>
#include <FL/filename.H>
#include <FL/Fl_Box.H>
#include <FL/Fl_Slider.H>
#include <string>
#include <FL/Fl_Int_Input.H>
#include <list>
#include <FL/Fl_Color_Chooser.H>
#include <vector>
#include <FL/Fl_Button.H>
#include <FL/Fl_Check_Button.H>
#include <FL/Fl_Tabs.H>
#include <FL/Fl_Input.H>
#include <FL/Fl_Menu_Bar.H>
#include <FL/Fl_Output.H>
#include <cstdlib>
using namespace std;
void Change_CB(Fl_Widget *w, void *) {
Fl_Menu_Bar *menu = (Fl_Menu_Bar*)w;
Fl_Menu_Item *p;
// Change submenu name
p = (Fl_Menu_Item*)menu->find_item("Edit/Submenu");
if (p) p->label("New Submenu Name");
// Change item name
p = (Fl_Menu_Item*)menu->find_item("Edit/New Submenu Name/Aaa");
if (p) p->label("New Aaa Name");
}
void Quit_CB(Fl_Widget *, void *) {
exit(0);
}
void head_window(Fl_Widget *, void *) {
Fl_Window *head = new Fl_Window(300, 200, "Head");
Fl_Input *input = new Fl_Input(110, 0, 100, 30, "Name");
Fl_Input *input1 = new Fl_Input(110, 40, 100, 30, "Part-Number");
Fl_Input *input2 = new Fl_Input(110, 80, 100, 30, "Weight");
Fl_Input *input3 = new Fl_Input(110, 120, 100, 30, "Cost");
Fl_Button *enter = new Fl_Button(110, 160, 100, 30, "Enter");
head->show();
}
void torso_window(Fl_Widget *, void *) {
Fl_Window *head = new Fl_Window(350, 250, "Torso");
Fl_Input *input = new Fl_Input(150, 0, 100, 30, "Name");
Fl_Input *input1 = new Fl_Input(150, 40, 100, 30, "Part-Number");
Fl_Input *input2 = new Fl_Input(150, 80, 100, 30, "Weight");
Fl_Input *input3 = new Fl_Input(150, 120, 100, 30, "Cost");
Fl_Input *input4 = new Fl_Input(150, 160, 100, 30, "Battery Compartments");
Fl_Button *enter = new Fl_Button(150, 200, 100, 30, "Enter");
head->show();
}
void locomotor_window(Fl_Widget *, void *) {
Fl_Window *head = new Fl_Window(350, 300, "Locomotor");
Fl_Input *input = new Fl_Input(150, 0, 100, 30, "Name");
Fl_Input *input1 = new Fl_Input(150, 40, 100, 30, "Part-Number");
Fl_Input *input2 = new Fl_Input(150, 80, 100, 30, "Weight");
Fl_Input *input3 = new Fl_Input(150, 120, 100, 30, "Cost");
Fl_Input *input4 = new Fl_Input(150, 160, 100, 30, "Speed(MPH)");
Fl_Input *input5 = new Fl_Input(150, 200, 100, 30, "Power Consumed(W)");
Fl_Button *enter = new Fl_Button(150, 240, 100, 30, "Enter");
head->show();
}
void arm_window(Fl_Widget *, void *) {
Fl_Window *head = new Fl_Window(350, 250, "Arm");
Fl_Input *input = new Fl_Input(150, 0, 100, 30, "Name");
Fl_Input *input1 = new Fl_Input(150, 40, 100, 30, "Part-Number");
Fl_Input *input2 = new Fl_Input(150, 80, 100, 30, "Weight");
Fl_Input *input3 = new Fl_Input(150, 120, 100, 30, "Cost");
Fl_Input *input4 = new Fl_Input(150, 160, 100, 30, "Power Consumed(W)");
Fl_Button *enter = new Fl_Button(150, 200, 100, 30, "Enter");
head->show();
}
void battery_window(Fl_Widget *, void *) {
Fl_Window *head = new Fl_Window(350, 250, "Battery");
Fl_Input *input = new Fl_Input(150, 0, 100, 30, "Name");
Fl_Input *input1 = new Fl_Input(150, 40, 100, 30, "Part-Number");
Fl_Input *input2 = new Fl_Input(150, 80, 100, 30, "Weight");
Fl_Input *input3 = new Fl_Input(150, 120, 100, 30, "Cost");
Fl_Input *input4 = new Fl_Input(150, 160, 100, 30, "Energy Stored");
Fl_Button *enter = new Fl_Button(150, 200, 100, 30, "Enter");
head->show();
}
void manual_window(Fl_Widget *, void *) {
Fl_Window *win = new Fl_Window(400, 275);
Fl_Text_Buffer *buff = new Fl_Text_Buffer();
Fl_Text_Display *disp = new Fl_Text_Display(20, 20, 350 , 250 , "Manual");
disp->buffer(buff);
win->resizable(*disp);
win->show();
buff->text("You may click the file button to open a dropdownnmenu and you will get a button to exit the windownor press CTRL + vn"
"You may click create and you get two options eithernto make a robot part or robot modeln"
"Clicking on robot part that shows the parts you mayncreate and their associated shortcutsn"
"If you have any questions click help then click onnmanual to open the manualn"
);
win->show();
}
int main() {
Fl_Window *win = new Fl_Window(600, 300, "Robot Shop" );
Fl_Menu_Bar *menu = new Fl_Menu_Bar(0, 0, 600, 25);
menu->add("File/Quit", FL_CTRL + 'v', Quit_CB);
menu->add("Create/Robot Part/Head" , FL_CTRL + 'h' , head_window );
menu->add("Create/Robot Part/Torso" , FL_CTRL + 't' , torso_window);
menu->add("Create/Robot Part/Arm" , FL_CTRL + 'a' , arm_window);
menu->add("Create/Robot Part/Battery" , FL_CTRL + 'b' , battery_window);
menu->add("Create/Robot Part/Locomotor" , FL_CTRL + 'l' , locomotor_window);
menu->add("Create/Robot Model");
menu->add("Help/Manual", FL_CTRL + 'm' , manual_window);
win->show();
return(Fl::run());
}
很抱歉,这是迟来的,但我认为下面的工作。
Fl_Input * inp1; //This is a global declaration
Fl_Button * sav1;
static void sav1_cb(Fl_Button *theButton, void*) // The callback function
{
string Surname = inp1 -> value();
cout << Surname;
}
inp1 = new Fl_Input (170+100, 50, 200, 20,"Surname"); // The input field which must be in a window
sav1 = new Fl_Button (170+310, 50 + 280, 100, 30,"Save"); // The save button
sav1->callback((Fl_Callback*)sav1_cb); //Attaching the callback to the save button
需要注意的是,"value()"函数是我用来操作在输入字段中存储或写入的数据的。这只是一个样本。希望能有所帮助。
您需要将数据存储在某个地方。建议使用
enum EData
{
EDHeadWeight,
EDHeadCost,
...
EDMax
};
std::string data[EDMax];
选项1:将Fl_Button (enter)更改为Fl_Return_Button,然后设置按钮的回调以保存所有值。这假定当用户按return时,他们已经输入了所有的值。
选项2:如果您想捕获每次输入时的enter,使用when(FL_WHEN_ENTER_KEY)
Fl_Input *input4 = new Fl_Input(150, 160, 100, 30, "Battery Compartments");
input4->when(FL_WHEN_ENTER_KEY);
input4->callback(grab, &data[EDTorsoBattery]);
...
void grab(Fl_Widget* w, void* param)
{
std::string& rv = *((std::string*)param);
Fl_Input* inp = dynamic_cast<Fl_Input*>(w);
rv = inp->value();
}
这里的问题是,它要求您将inp->value()存储到const char*中,但常量不能修改…
相关文章:
- 如何使用堆栈将字符串输入拉伸 k 次
- 不确定出了什么问题(字符串输入)
- 如何获取字符串输入n次?
- 使用重定向命令从 stdin 读入的字符串"<"输入文件未正确附加
- isPalindrome不显示输出,isPalindrome函数未使用字符串输入作为字符串参数进行测试
- C++ 对象数组字符串输入在控制台上不起作用
- 为什么 cin 在包含字符串标头后接受字符串输入
- 拆分字符串输入并查找字符是否有效
- 有没有办法将最初作为字符串输入的数字字符数组复制到双向链表中
- 将字符串输入到多维数组中
- 字符串输入后的空格
- 字符串流中的自定义字符串输入
- C 未验证字符串输入
- 总结数字字符串输入的最佳方法
- 如何验证用户将字符串输入到std :: cin中
- 在C++测试家庭作业问题中的字符串输入时无法退出循环(作为数据验证)
- 如何在C++中检测字符串输入中的换行符
- C++将字符串输入拆分为两个整数
- 如何检查字符串输入是否为有效的双精度
- 在读取数据文件时,如何找到用户字符串输入的字谜?