Guidance
指路人
g.yi.org
Guidance Forums / wxWidgets (wxWindows) in C++ / Connecting Events Between Classes

Register 
注册
Search 搜索
首页 
Home Home
Software
Upload

  
Forum List • Thread List • Reply • Refresh • New Topic • Search • Previous • Next First 1 Last
Message1. Connecting Events Between Classes
#4736
Posted by: 2004-07-07 12:50:24
I have class MyFrame : public wxFrame that is associated with (and the parent of) a number of other classes.  Each of these other classes (controls) is an object on the frame (such as an image or text).  I would like my parent class to process only the EVT_LEFT_UP event for all of these controls.  I currently give each control a unique id, and I am attempting to connect them using the following line:


Connect(UNIQUE_ID, wxEVT_LEFT_UP, (wxObjectEventFunction) (wxEventFunction) (wxCommandEventFunction) MyFrame::OnUniqueIDMouseUp );

The problem is that the function OnUniqueIDMouseUp never gets called.  I've noticed that trying the above statement with the event type of "EVT_LEFT_UP" as opposed to "wxEVT_LEFT_UP" will not compile.  I am not sure if this may be a source of the error or not.  What could I be doing wrong?  Or is there a better way of going about this?
Message2. Re: Connecting Events Between Classes
#4739
Posted by: upCASE 2004-07-07 15:11:38
Hi!
"EVT_LEFT_UP" is a macro while "wxEVT_LEFT_UP" is a #define, so "wxEVT_LEFT_UP" is the correct one :)

Maybe the following could work:
1. Setup the event table for the frame as you would do normally.
2. Create the controls and pass them a pointer to the parent (like you would normally do)
3. Use wxWindow::SetEventHandler() or better wxWindow::PushEventHandler() with the parent as argument.

Since wxFrame is a wxWindow and that in turn derived from wxEvtHandler it should work. Another option would be to derive your own event handling class from wxEvtHandler and use PushEventHandler() on the controls.

Else: Have you tried using Connect() like parent->Connect() ?
upCASE
-----------------------------------
If it was hard to write, it should be hard to read!- Do. Or do not. There is no try!
Message3. Re: Connecting Events Between Classes
#4761
Posted by: 2004-07-08 12:57:19
Thanks upCASE,

Using that idea I made a custom EvtHandler Class.  This had a pointer back to the parent window.  When I created my controls I passed my EvtHandler class to my controls and they PushedEventHandler.  When the wxEVT_LEFT_UP was triggered, my EvtHandler class caught it, (by putting an EVT_LEFT_UP macro into the EvtHandler static event table), and then it used its pointer to the parent window to call the appropriate function.  I also used:
MyControl* clickedObj = (MyControl*)event.GetEventObject();
from within the EvtHandler function to figure out which control had been clicked.

Awesome.  Thanks man.
  
Forum List • Thread List • Reply • Refresh • New Topic • Search • Previous • Next First 1 Last
掌柜推荐
 
 
 
 
 
 
 
 
 
 
 
 
© Thu 2022-8-18  Guidance Laboratory Inc.
Email:webmaster1g.yi.org Hits:0