问题描述
无法直接从我的代码调用事件处理程序。我在两年前发现了同样的问题。原始问题
但是行
me_InsertCommentText(wxCommandEvent());
没有编译(mingw32-gcc 4.8,win7,codeblocks,wxFormBuilder)
如何使这个工作?
关于使用命名临时变量的答案在技术上是正确的,但是重要的是你真的不应该这样做。处理程序只应该从wxWidgets调用,而不是直接调用一些 OnFoo(wxFooEvent&)
,您应该重构代码,只需调用一些新的来自
,然后从其余部分调用 OnFoo()
的DoFoo() DoFoo()
你的代码如果你需要它。
当使用C ++ 11时,这变得更加简单,因为你甚至不需要 OnFoo()
在这种情况下,只能写
whatever-> Bind(wxEVT_FOO,[= ](wxCommandEvent&){DoFoo();});
以避免额外的功能。
Having trouble to call an event handler directly from my code. I found the same question 2 years ago here.original question
But the lineme_InsertCommentText(wxCommandEvent());
is not compiling (mingw32-gcc 4.8, win7, codeblocks, wxFormBuilder)
For me that appears to be caused by the calling by reference parameters.How can I get this to work?
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.
这篇关于直接调用事件处理程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!