wxWidgets 绑定示例

发布于 2024-12-29 10:35:15 字数 2299 浏览 0 评论 0原文

我正在使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

幸福不弃 2025-01-05 10:35:16

结果编译器抱怨我正在使用的事件类型(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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文