直接调用事件处理器
Calling event handler directly
无法直接从我的代码调用事件处理程序。 2 年前我在这里发现了同样的问题。
original question
但是线
me_InsertCommentText(wxCommandEvent());
未编译(mingw32-gcc 4.8、win7、codeblocks、wxFormBuilder)
error: no matching function for call to 'mjpgen_wdDialog::me_InsertCommentText(wxCommandEvent)'
note: candidate is:
note: void mjpgen_wdDialog::me_InsertCommentText(wxCommandEvent&)
对我来说,这似乎是由引用参数调用引起的。
我怎样才能让它工作?
wxCommandEvent()
是一个临时对象,不能绑定到非常量引用。您可以在此处使用命名变量:
wxCommandEvent event;
me_InsertCommentText(event);
或者将参数类型改为const reference:
void mjpgen_wdDialog::me_InsertCommentText(const wxCommandEvent&)
然后
me_InsertCommentText(wxCommandEvent());
关于使用命名临时变量的答案在技术上是正确的,但重要的是你真的不应该首先这样做。处理程序只应该从 wxWidgets 调用,而不是直接调用一些 OnFoo(wxFooEvent&)
你应该重构你的代码,只从 OnFoo()
调用一些新的 DoFoo()
然后调用 DoFoo()
如果需要,可以从其余代码中获取。
这在使用 C++11 时变得更加简单,因为在这种情况下您甚至根本不需要 OnFoo()
,只需编写
whatever->Bind(wxEVT_FOO, [=](wxCommandEvent&) { DoFoo(); });
避免额外的功能。
无法直接从我的代码调用事件处理程序。 2 年前我在这里发现了同样的问题。 original question
但是线
me_InsertCommentText(wxCommandEvent());
未编译(mingw32-gcc 4.8、win7、codeblocks、wxFormBuilder)
error: no matching function for call to 'mjpgen_wdDialog::me_InsertCommentText(wxCommandEvent)' note: candidate is: note: void mjpgen_wdDialog::me_InsertCommentText(wxCommandEvent&)
对我来说,这似乎是由引用参数调用引起的。 我怎样才能让它工作?
wxCommandEvent()
是一个临时对象,不能绑定到非常量引用。您可以在此处使用命名变量:
wxCommandEvent event;
me_InsertCommentText(event);
或者将参数类型改为const reference:
void mjpgen_wdDialog::me_InsertCommentText(const wxCommandEvent&)
然后
me_InsertCommentText(wxCommandEvent());
关于使用命名临时变量的答案在技术上是正确的,但重要的是你真的不应该首先这样做。处理程序只应该从 wxWidgets 调用,而不是直接调用一些 OnFoo(wxFooEvent&)
你应该重构你的代码,只从 OnFoo()
调用一些新的 DoFoo()
然后调用 DoFoo()
如果需要,可以从其余代码中获取。
这在使用 C++11 时变得更加简单,因为在这种情况下您甚至根本不需要 OnFoo()
,只需编写
whatever->Bind(wxEVT_FOO, [=](wxCommandEvent&) { DoFoo(); });
避免额外的功能。