Unpin lambda event handler

How can I untie the event handler that I bound as shown below?

MyFrame::MyFrame()
{
    Bind(wxEVT_COMMAND_MENU_SELECTED,
         [](wxCommandEvent&) {
            // Do something useful
         },

         wxID_EXIT);
}

Thanks so much for the first answer. I have added additional information.

The ability to unleash an event handler using a specific Functor is documented and works fine, but if you use the C ++ 11 lambda style to bind somthing, there is no Functor availibale to call the unbind method later. And this causes problems if the corresponding wxEvtHandlermust be destroyed.

Is there a "trick". If not, I don't see any real use for binding using lambda functors. I hope I'm wrong.

Thanks a lot Hacki

+3
source share
4

, Bind , . , , .

class MyFrame 
{ 
    ... 
    std::function<void()> DoUnbind;
}

MyFrame::MyFrame()
{
    auto DoSomethingUseful = [](wxCommandEvent&) {
            // Do something useful
         };

    Bind(wxEVT_COMMAND_MENU_SELECTED,
         DoSomethingUseful,
         wxID_EXIT);

    DoUnbind = [DoSomethingUseful](){
        Unbind(wxEVT_COMMAND_MENU_SELECTED,
               DoSomethingUseful,
               wxID_EXIT);
    };
}
-2

wxWidget Unbind ( lambdas ):

, , , , -.

, , Unbind - , , Bind Unbind, ( const-, , ).

lambdas, , ++ 11. Unbind -, . , . .

, :

static auto event_handler = [](wxCommandEvent&) {
  // Do something
};
// ...
Bind(..., event_handler, ...);
// ...
Unbind(..., event_handler, ...);

( ):

struct EventHandler {
  void operator()(wxCommandEvent&) const {
    // Do something
  }
};
// ...
Bind(..., EventHandler(), ...);
// ...
Unbind(..., EventHandler(), ...);
+3

, , , , .

auto const handler = [](wxCommandEvent&) { ... };

// To bind it:
Bind(wxEVT_MENU, handler, wxID_EXIT);

// To unbind it:
Unbind(wxEVT_MENU, handler, wxID_EXIT);
+1

When the program completes execution, all event handlers will be disconnected from all controls. This is true even for lambda.

Very rarely, you need to undo something inside the user code, but if you respond to @Caleth, it will work.

0
source

Source: https://habr.com/ru/post/1676598/


All Articles