Calling event handler directly

后端 未结 2 1607
渐次进展
渐次进展 2021-01-25 09:04

Having trouble to call an event handler directly from my code. I found the same question 2 years ago here. original question

But the line me_InsertCommentText(wxCo

相关标签:
2条回答
  • 2021-01-25 09:51

    wxCommandEvent() is a temporary object, which couldn't be bound to non-const reference. You could use named variable here:

    wxCommandEvent event;
    me_InsertCommentText(event);
    

    or change the type of parameter to const reference:

    void mjpgen_wdDialog::me_InsertCommentText(const wxCommandEvent&)
    

    then

    me_InsertCommentText(wxCommandEvent());
    
    0 讨论(0)
  • 2021-01-25 10:04

    The answer about using a named temporary variable is technically correct, but the important thing is that you really shouldn't be doing this in the first place. The handlers are only supposed to be called from wxWidgets and instead of calling some OnFoo(wxFooEvent&) directly you should refactor your code to just call some new DoFoo() from OnFoo() and then call DoFoo() from the rest of your code if you need it.

    This becomes even simpler when using C++11 as you don't even need to have OnFoo() at all in this case and could just write

    whatever->Bind(wxEVT_FOO, [=](wxCommandEvent&) { DoFoo(); });
    

    to avoid an extra function.

    0 讨论(0)
提交回复
热议问题