Search code examples
c++eventswxwidgets

wxWidgets Bind Example


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.


Solution

  • 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.