Page 1 of 1

wxTimer question

Posted: Thu Nov 22, 2007 4:09 pm
by cr0nholio
I am trying to put a wxTimer into an already existing application. The GUI is built using wxFormBuilder. I tried following the example given here.

Code: Select all

class MyFrame : public wxFrame
{
public:
    ...
    void OnTimer(wxTimerEvent& event);

private:
    wxTimer m_timer;
};

BEGIN_EVENT_TABLE(MyFrame, wxFrame)
    EVT_TIMER(TIMER_ID, MyFrame::OnTimer)
END_EVENT_TABLE()

MyFrame::MyFrame()
       : m_timer(this, TIMER_ID)
{
    m_timer.Start(1000);    // 1 second interval
}

void MyFrame::OnTimer(wxTimerEvent& event)
{
    // do whatever you want to do every second here
}
However, I cannot add the timer to the GUIFrame class (inherits from wxFrame) that I get from wxFormBuilder because the GUIFrame.h file will be overwritten each time I change the GUI. So, I tried adding it to my application's main class which in turn inherits from GUIFrame.

That generates compile errors for the event table code:

Code: Select all

error: ISO C++ does not permit wxFrameBase::sm_eventTable to be defined as myAppFrame::sm_eventTable
error: const wxEventTableEntry wxFrameBase::sm_eventTableEntries [] is private within this context
error: no const wxEventTable* myAppFrame::GetEventTable() const member function declared in class myAppFrame
And a similar one for HashTable. Plus, I can't do this:

Code: Select all

myAppFrame::myAppFrame()
       : m_timer(this, TIMER_ID)
Because it's supposed to inherit from GUIFrame:

Code: Select all

myAppFrame::myAppFrame(wxFrame *frame)
   : GUIFrame(frame)
So what am I doing wrong?

Posted: Thu Nov 22, 2007 5:37 pm
by meisjohn
Did you declare the event table for myAppFrame using the "DECLARE_EVENT_TABLE()" macro?

It looks as though when you begin the event table, it thinks you're trying to override the base class's event table.

John.

Re: wxTimer question

Posted: Thu Nov 22, 2007 9:10 pm
by tierra
As for you're initialization list, it can be done like this:

Code: Select all

MyFrame::MyFrame()
: GUIFrame(frame),
  m_timer(this, TIMER_ID)

Posted: Fri Nov 23, 2007 8:33 am
by cr0nholio
Thanks guys! It works now. I must admit I never quite got the hang of event tables because wxFormBuilder was taking care of that until now. :wink: