I'm on Ubuntu. I need to return a function when I click in a dropdown list, but don't understand how to do that. For example:
...
dropdownList.set_active_text("Choose");
dropdownList.signal_changed().connect(sigc::mem_fun(*this, &usb_boot::showing));
std::cout << "var = " << var << std::endl;
...
void usb_boot::showing(){
Gtk::MessageDialog dialogue(*this, dropdownList.get_active_text());
dialog.set_secondary_text("Choose list");
dialog.run();
std::cout << "You choose :\n" << dropdownList.get_active_row_number() << " " << dropdownList.get_active_text() << std::endl;
add return here ?
How to return dropdownList.get_active_text()
and dropdownList.get_active_row_number()
to variables ?
If I understand your question correctly, you want to reuse some values you get from within your handler (i.e. showing
) outside of its scope. To do this, I would suggest using a lambda expression and capture by reference your variables. For example:
#include <iostream>
#include <gtkmm.h>
int main(int argc, char **argv)
{
auto app = Gtk::Application::create(argc, argv, "so.question.q63850579");
Gtk::Window window;
Gtk::ComboBoxText combo;
combo.append("option 1");
combo.append("option 2");
combo.append("option 3");
// These are your variables, which you want to set to the values the user will choose. They are defined
// outise the handler:
std::string choice;
int row;
// Here you set the handler. The variables 'choice', 'row' and 'combo' are all passed by reference to
// the handler (notice the '&'):
combo.signal_changed().connect([&choice, &row, &combo](){
choice = combo.get_active_text();
row = combo.get_active_row_number();
// Inside the scope of the handler, we can see the variable content changing everytime the user
// changes a value:
std::cout << "Your current selection is item #" << row << ", which is: " << choice << std::endl;
});
window.add(combo);
window.show_all();
int returnCode = app->run(window);
// When the window closes, the variables are read again, but this time from outside the handler's
// scope. This is possible because they were references:
std::cout << "Your final selection was item #" << row << ", which is: " << choice << std::endl;
return returnCode;
}
Another option, since you use sigc::mem_fun
, would be to save the contents inside a member variable of the class that represents the object pointed to by this
. I personally prefer the lambda expression, since these values are propably unrelated to the class.