SetButtonAction sets action for a button identified by 'button' parameter. The action is an object of type ButtonAction, which is defined in class ApplicationDefines as follows:
using ButtonAction = std::function<void()>;
This means that the action must be a callable object (object with operator() or a function) with no parameters. An example would be:
void func()
{
puts("Ok button pressed");
}
class MyClass
{
public:
void ShowMessage()
{
...
}
};
Often we want the function to have some parameters we need to use in the function. In that case, std::bind should be used. It binds parameters to a function before it is called, and returns a function with corresponding signature. For example:
void func(Widget *widget, int width, int height)
{
widget->SetSize(width, height);
}
class MyClass
{
private:
Widget *widget;
public:
void ShowMessage()
{
...
}
};
func() can also be a member function (which is better if we want to call private member functions or use private members). If the function is non-static, the first argument in std::bind call is the object on which we want to call the function. Example:
class Widget
{
private:
int width;
int height;
void SetSize(int w, int h)
{
width = w;
height = h;
}
static void NotResizedInfo()
{
puts("Widget was not resized!");
}
public:
void ShowMessage()
{
}
};
If the function is small, we can also use lambda functions. Setting an action for cancel button from the previous example can be replaced with:
If the lambda function is bigger, it makes the code less readable, so a normal function (or member function) should be used instead.
- Parameters
-
| button | - button for which the action is set |
| action | - action for the button |