wxWidgets 绑定示例
我正在使用 wxWidgets 2.9,但在使用 Bind() 函数时遇到问题。 wxEvtHandler 的 文档 对
void Bind (const EventTag &eventType, Functor functor, int id=wxID_ANY, int lastId=wxID_ANY, wxObject *userData=NULL)
我来说,这意味着我输入类似这样
Bind(wxEVT_PAINT, &Board::onPaint);
或这样的
Bind(wxEVT_TIMER, &TetrisController::onTimer, ID_TIMER);
内容,但两者都没有这些工作在我的程序中。 wxWidgets 还有一个具有不同格式的事件的 解释
Bind(wxEVT_COMMAND_MENU_SELECTED, &MyFrame::OnExit, this, wxID_EXIT);
Bind(wxEVT_COMMAND_MENU_SELECTED, &MyFrameHandler::OnFrameExit, &myFrameHandler, wxID_EXIT);
: () 函数在列出 ID 之前需要一个指向具有函子的对象的指针。 我尝试过
Bind(wxEVT_PAINT, &Board::onPaint, this); // this points to the Board
Bind(wxEVT_TIMER, &TetrisController::onTimer, controllerPtr, ID_TIMER);
这些都不起作用。 我可以获得如何正确使用 Bind() 函数的示例吗?这个函数做错了什么?
编辑: 发布更多代码希望得到答案。以下是我收到的错误消息:
版本#1
error: must use '.*' or '->*' to call pointer-to-member function in '((wxEventFunctorFunctor<wxEventTypeTag<wxPaintEvent>, void (tetris::Board::*)(wxPaintEvent&)>*)this)->wxEventFunctorFunctor<wxEventTypeTag<wxPaintEvent>, void (tetris::Board::*)(wxPaintEvent&)>::m_handler (...)', e.g. '(... ->* ((wxEventFunctorFunctor<wxEventTypeTag<wxPaintEvent>, void (tetris::Board::*)(wxPaintEvent&)>*)this)->wxEventFunctorFunctor<wxEventTypeTag<wxPaintEvent>, void (tetris::Board::*)(wxPaintEvent&)>::m_handler) (...)'|
版本#2
error: no matching function for call to 'wxEventFunctorMethod<wxEventTypeTag<wxTimerEvent>, TetrisController, wxCommandEvent, TetrisController>::CheckHandlerArgument(wxTimerEvent*)'
error: cannot convert 'Board*' to 'TetrisController*' in initialization
我也尝试过
Bind(wxEVT_TIMER, &TetrisController::onTimer, this, ID_TIMER); // this points to the Board
,但出现第二个错误。我真的很想知道如何正确使用 Bind() 函数。
I'm using wxWidgets 2.9 and I'm having trouble with the Bind() function. The documentation for wxEvtHandler says
void Bind (const EventTag &eventType, Functor functor, int id=wxID_ANY, int lastId=wxID_ANY, wxObject *userData=NULL)
To me, this means I enter something like this
Bind(wxEVT_PAINT, &Board::onPaint);
or this
Bind(wxEVT_TIMER, &TetrisController::onTimer, ID_TIMER);
but neither of these work in my program.
wxWidgets also has an explanation of events that has a different format:
Bind(wxEVT_COMMAND_MENU_SELECTED, &MyFrame::OnExit, this, wxID_EXIT);
Bind(wxEVT_COMMAND_MENU_SELECTED, &MyFrameHandler::OnFrameExit, &myFrameHandler, wxID_EXIT);
It seems the Bind() function requires a pointer to the object that has the functor before listing the ID.
I tried
Bind(wxEVT_PAINT, &Board::onPaint, this); // this points to the Board
Bind(wxEVT_TIMER, &TetrisController::onTimer, controllerPtr, ID_TIMER);
Neither of these work either.
Can I get an example of how to properly use the Bind() function? What am doing wrong with this function?
EDIT:
Posting more code in hopes of getting an answer. Here's the error messages I'm getting:
Version #1
error: must use '.*' or '->*' to call pointer-to-member function in '((wxEventFunctorFunctor<wxEventTypeTag<wxPaintEvent>, void (tetris::Board::*)(wxPaintEvent&)>*)this)->wxEventFunctorFunctor<wxEventTypeTag<wxPaintEvent>, void (tetris::Board::*)(wxPaintEvent&)>::m_handler (...)', e.g. '(... ->* ((wxEventFunctorFunctor<wxEventTypeTag<wxPaintEvent>, void (tetris::Board::*)(wxPaintEvent&)>*)this)->wxEventFunctorFunctor<wxEventTypeTag<wxPaintEvent>, void (tetris::Board::*)(wxPaintEvent&)>::m_handler) (...)'|
Version #2
error: no matching function for call to 'wxEventFunctorMethod<wxEventTypeTag<wxTimerEvent>, TetrisController, wxCommandEvent, TetrisController>::CheckHandlerArgument(wxTimerEvent*)'
error: cannot convert 'Board*' to 'TetrisController*' in initialization
I also tried
Bind(wxEVT_TIMER, &TetrisController::onTimer, this, ID_TIMER); // this points to the Board
and I get the second error. I would really like to know how to properly use the Bind() function.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
结果编译器抱怨我正在使用的事件类型(wxCommandEvent)。当我将其更改为 wxTimerEvent 时,版本 #2 开始工作。
Turns out the compiler was complaining about the type of event I was using (wxCommandEvent). When I changed it to wxTimerEvent, version #2 started working.