Discussion:
Using PushEventHandler
Mauro Ziliani
2014-03-11 22:35:11 UTC
Permalink
Hi all.
My question is about PushEventHandler.

I need to add the same event handler to many wxWindow objects.
So I think of define a wxEvtHandler derived class, and push it into
wxWindows I need it.

Example:

class MyEventHandler : public wxEvtHandler
{
public:
MyEventHandler(wxWindow *container)
{
m_container = container;
Bind(wxEVT_MY_EVENT, &MyEventHandler::on_ioEvent, this);
}
~MyEventHandler(wxWindow *container)
{
Unbind(wxEVT_MY_EVENT, &MyEventHandler::on_ioEvent, this);
}

protected:
void on_ioEvent(wxNotifyEvent& event)
{
wxWindowList& children = m_container->GetChildren();
wxWindowList::iterator child;
for (child=children.begin(); child!=children.end())
{
wxStaticText *st = wxDynamicCast( *child, wxStaticText );
if (st)
st->SetLabelText(event.GetString());
}
}
};

class Display : public wxPanel
{
public:
Display(wxWindow *parent): wxPanel(parent, -1)
{
PushEventHandler( new MyEventHandler(this) );
}

~Display
{
PopEventHandler(true);
}
};


Inside a timer event I send the wxNotifyEvent to the display object

class MainFrame : public wxFrame
{
public:
MainFrame() : wxFrame(NULL, -1, "Test IO"), m_timer(this, 1)
{
m_display = new Display(this);
Bind(wxEVT_TIMER, &MainFrame::on_timerEvent, this);

m_timer.Start(1000);
}

~MainFrame()
{
Unbind(wxEVT_TIMER, &MainFrame::on_timerEvent, this);
}
private:
wxTimer m_timer;
Display *m_display;

void on_timereEvent(wxTimerEvent& event)
{
wxNotifyEvent evt_io_values(wxEVT_MY_EVENT);
evt_io_value.SetString("test");
wxQueueEvent(m_display, evt_io_values.Clone());
}
};


The matter is: the pushed event handlder is not executed.

Where is the mistake?

Mauro
--
Please read http://www.wxwidgets.org/support/mlhowto.htm before posting.

To unsubscribe, send email to wx-users+***@googlegroups.com
or visit http://groups.google.com/group/wx-users
Vadim Zeitlin
2014-03-13 12:58:11 UTC
Permalink
On Tue, 11 Mar 2014 23:35:11 +0100 Mauro Ziliani wrote:

MZ> My question is about PushEventHandler.

Without reading further, you really shouldn't use it any more. It was
useful before Connect() and Bind() were added, it (almost?) never is now.

MZ> I need to add the same event handler to many wxWindow objects.

Just call Bind() on all of them with the same handler.

Regards,
VZ
--
TT-Solutions: wxWidgets consultancy and technical support
http://www.tt-solutions.com/
Mauro Ziliani
2014-03-14 07:25:32 UTC
Permalink
Thanks for the answer.

I see where is my mistunderstanding.

The wxQueueEvent(m_display, ...) is wrong for purpose is need.
This put the event only in the original eveht handler of Display class.
While I need to run all event handlers in stack.

So the right calls of wxQUeueEvent must be

wxQueueEvent(m_display->GetEventHandler(), ...)

This ensure that all event handlers in the stack will be processed from top
to bottom.

Thanks again.
Mauro
Post by Mauro Ziliani
Hi all.
My question is about PushEventHandler.
I need to add the same event handler to many wxWindow objects.
So I think of define a wxEvtHandler derived class, and push it into
wxWindows I need it.
class MyEventHandler : public wxEvtHandler
{
MyEventHandler(wxWindow *container)
{
m_container = container;
Bind(wxEVT_MY_EVENT, &MyEventHandler::on_ioEvent, this);
}
~MyEventHandler(wxWindow *container)
{
Unbind(wxEVT_MY_EVENT, &MyEventHandler::on_ioEvent, this);
}
void on_ioEvent(wxNotifyEvent& event)
{
wxWindowList& children = m_container->GetChildren();
wxWindowList::iterator child;
for (child=children.begin(); child!=children.end())
{
wxStaticText *st = wxDynamicCast( *child, wxStaticText );
if (st)
st->SetLabelText(event.GetString());
}
}
};
class Display : public wxPanel
{
Display(wxWindow *parent): wxPanel(parent, -1)
{
PushEventHandler( new MyEventHandler(this) );
}
~Display
{
PopEventHandler(true);
}
};
Inside a timer event I send the wxNotifyEvent to the display object
class MainFrame : public wxFrame
{
MainFrame() : wxFrame(NULL, -1, "Test IO"), m_timer(this, 1)
{
m_display = new Display(this);
Bind(wxEVT_TIMER, &MainFrame::on_timerEvent, this);
m_timer.Start(1000);
}
~MainFrame()
{
Unbind(wxEVT_TIMER, &MainFrame::on_timerEvent, this);
}
wxTimer m_timer;
Display *m_display;
void on_timereEvent(wxTimerEvent& event)
{
wxNotifyEvent evt_io_values(wxEVT_MY_EVENT);
evt_io_value.SetString("test");
wxQueueEvent(m_display, evt_io_values.Clone());
}
};
The matter is: the pushed event handlder is not executed.
Where is the mistake?
Mauro
--
Please read http://www.wxwidgets.org/support/mlhowto.htm before posting.

To unsubscribe, send email to wx-users+***@googlegroups.com
or visit http://groups.google.com/group/wx-users
Loading...