直接调用事件处理程序 [英] Calling event handler directly

查看:137
本文介绍了直接调用事件处理程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

无法直接从我的代码调用事件处理程序。我在两年前发现了同样的问题。
原始问题



但是行
me_InsertCommentText(wxCommandEvent());
没有编译(mingw32-gcc 4.8,win7,codeblocks,wxFormBuilder)


错误:没有匹配的函数调用'mjpgen_wdDialog :: me_InsertCommentText (wxCommandEvent)'
注意:候选人是:
注意:void mjpgen_wdDialog :: me_InsertCommentText(wxCommandEvent&)




如何使这个工作?

解决方案

关于使用命名临时变量的答案在技术上是正确的,但是重要的是你真的不应该这样做。处理程序只应该从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 line me_InsertCommentText(wxCommandEvent()); is not compiling (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&)

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.

这篇关于直接调用事件处理程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆