Cannot pass lambda expression as Button's on_click()
function
#323
-
I'm not sure if this is a bug or just me being dense (relearning C++, last time I touched it was pre-C++11), but it seems I can't pass a lambda as Button's Component exit_button = Button( "Exit", screen.ExitLoopClosure() );
Component exit_button = Button( "Exit", [&screen]{ screen.ExitLoopClosure(); } ); The first example will work, while the latter won't. I've looked into Would it some cases make code easier to write I could pass lambdas directly. |
Beta Was this translation helpful? Give feedback.
Replies: 3 comments 3 replies
-
Hello,
So you should use Component exit_button = Button( "Exit", screen.ExitLoopClosure()); or: Component exit_button = Button( "Exit", [&screen] { screen.ExitLoopClosure()(); }); |
Beta Was this translation helpful? Give feedback.
-
Hello, auto screen = ftxui::ScreenInteractive::FitComponent();
Cgui cgui(screen);
cgui.showdialoge();
std::cout << "Delete keys " << cgui.selected_item << "\n"; and in Cgui Cgui::Cgui(ftxui::ScreenInteractive &screen) : selected_index(0), screen_(screen) {
}
void Cgui::showdialoge() {
std::vector<std::string> key = {
"DKLK4PM.TEST.WPG",
"DKLK4XAC.WEST",
"DKLK4PM.TEST.WEST",
};
auto button_del = ftxui::Button("Keys", [&key, this] {
Selected_key(key[selected_index]);
});
screen_.Loop(ftxui::Container::Vertical({
ftxui::Dropdown(&key, &selected_index),
button_del,
}));
}
void Cgui::Selected_key(std::string &key) {
selected_item = key;
screen_.ExitLoopClosure();
// want to return to main, do not work, why?
} appreciate any help. |
Beta Was this translation helpful? Give feedback.
-
I am very grateful for your help. |
Beta Was this translation helpful? Give feedback.
Hello,
ScreenInteractive::ExitLoopClosure()
returns the closure. It does not execute it.So you should use
Component exit_button = Button( "Exit", screen.ExitLoopClosure());
or: