Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to connect a wxWidgets timer to an event using wxEvtHandler::Connect

Tags:

c++

wxwidgets

I have been trying to connect my timer to an a function. In my derived class What i am doing is

Timer->SetOwner(this,wxID_Timer);
Timer->Connect(wxID_Timer,wxTimerEventHandler( Window::OnUpdate ), NULL, this );

Where my declaration of OnUpdate is

void OnUpdate( wxTimerEvent& event );

Can any one tell me what is wrong here, why is OnUpdate not being called periodically after i start the timer?. Please and Thank you.

Also I am not using static event Tables.The other answer on wxTimer is not helping me.

like image 892
xeon111 Avatar asked Jun 09 '11 15:06

xeon111


3 Answers

After a bit of searching i actually found the answer, no one is answering so i am posting it here. There were a few things that i was doing wrong. i. I actually had to connect the class to my timer . That meant i had to do something like

this->Connect(Timer.GetId(),wxEVT_TIMER,wxTimerEventHandler( Window::OnUpdate ), NULL, this );

Where wxEVT_TIMER was the event type.

Similarly to disconnect

this->Disconnect(wxID_Timer,wxEVT_TIMER,wxTimerEventHandler( Window::OnUpdate ), NULL, this );
like image 148
xeon111 Avatar answered Nov 20 '22 16:11

xeon111


There seems to be some confusion here so let me try to clear it.

You can either call timer->Connect(...) on the timer itself or use SetOwner(frame) and then do frame->Connect(...).

Calling SetOwner() and then calling Connect() on the timer doesn't make much sense as SetOwner() ensures that the timer event is delivered to the owner directly. However by default there is no owner and the timer sends the events to itself which is why without SetOwner() call you must call Connect() on the timer.

like image 4
VZ. Avatar answered Nov 20 '22 14:11

VZ.


The answer did not work for me. This is what I did instead.

You first must let your frame own the events that the wxtimer emits.

m_timer.SetOwner( this );

Then you can have your frame's event handler handle the events. If you only have one timer, use this.

this->Connect( wxEVT_TIMER, wxTimerEventHandler( Frame::OnTimer ), NULL, this );

If you have multiple timers, use this to connect each timer's event to a different function.

this->Connect( m_timer.GetId(), wxEVT_TIMER, wxTimerEventHandler( Frame::OnTimerForSpecificTimer ), NULL, this );

Doing this will allow the frame to put the timer event in its event queue and process it when it can.

like image 3
Andrew Avatar answered Nov 20 '22 16:11

Andrew