使用下拉列表小部件时如何 return 函数值
How to return a function value when using a dropdown list widget
我在 Ubuntu。当我在下拉列表中单击时,我需要 return 一个函数,但不明白该怎么做。例如:
...
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 ?
如何return dropdownList.get_active_text()
和 dropdownList.get_active_row_number()
到变量?
如果我对你的问题的理解正确,你想在处理程序范围之外重用你从处理程序(即 showing
)中获得的一些值。为此,我建议使用 lambda 表达式并通过引用捕获变量。例如:
#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;
}
由于您使用 sigc::mem_fun
,另一种选择是将内容保存在 class 的成员变量中,该成员变量表示 this
指向的对象。我个人更喜欢 lambda 表达式,因为这些值可能与 class.
无关
我在 Ubuntu。当我在下拉列表中单击时,我需要 return 一个函数,但不明白该怎么做。例如:
...
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 ?
如何return dropdownList.get_active_text()
和 dropdownList.get_active_row_number()
到变量?
如果我对你的问题的理解正确,你想在处理程序范围之外重用你从处理程序(即 showing
)中获得的一些值。为此,我建议使用 lambda 表达式并通过引用捕获变量。例如:
#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;
}
由于您使用 sigc::mem_fun
,另一种选择是将内容保存在 class 的成员变量中,该成员变量表示 this
指向的对象。我个人更喜欢 lambda 表达式,因为这些值可能与 class.