FLTK Fl_Choice 获取选中项
Posted
技术标签:
【中文标题】FLTK Fl_Choice 获取选中项【英文标题】:FLTK Fl_Choice obtain selected item 【发布时间】:2014-05-22 05:38:12 【问题描述】:我想获取选定的用户项,但此代码出错:
choice->value();
希望各位好心人能帮帮我。
Fl_Choice* choice;
int i = 0;
void but_cb(Fl_Button* obj, void*)
i = choice->value();
cout<<i;
int main (int argc, char* argv[])
Fl_Double_Window* win = new Fl_Double_Window(400,400,"Sample");
win->begin();
Fl_Choice* choice = new Fl_Choice(100,100,100,100,"Name");
choice->add("Peter");
choice->add("Tom");
choice->add("Mary");
Fl_Button* but = new Fl_Button(300,300,50,50,"Selected");
but->callback((Fl_Callback*)but_cb);
win->show();
return (Fl::run());
【问题讨论】:
【参考方案1】:我没有花太多时间阅读代码,因此下面的修改和注释代码可能无法解决您的问题。我相信 choice
在 but_cb()
中为 NULL,因为您正在 main() 函数中重新定义选择,隐藏全局“选择”指针。
Fl_Choice* choice;
int i = 0;
void but_cb(Fl_Button* obj, void*)
i = choice->value(); // uses global `choice` pointer to Fl_Choice.
cout << i;
int main (int argc, char* argv[])
Fl_Double_Window* win = new Fl_Double_Window(400,400,"Sample");
win->begin();
/* You are redefining choice here! BAD, because but_cb uses the global one.
Fl_Choice* choice = new Fl_Choice(100,100,100,100,"Name");
*/
// Good, assigns to the global `choice`, so it won't be NULL when but_cb is called
choice = new Fl_Choice(100,100,100,100,"Name");
choice->add("Peter");
choice->add("Tom");
choice->add("Mary");
Fl_Button* but = new Fl_Button(300,300,50,50,"Selected");
but->callback((Fl_Callback*)but_cb);
win->show();
return (Fl::run());
【讨论】:
以上是关于FLTK Fl_Choice 获取选中项的主要内容,如果未能解决你的问题,请参考以下文章