Passing data between controls
-
- Experienced Solver
- Posts: 52
- Joined: Thu Dec 08, 2016 2:42 pm
Passing data between controls
Not sure if this is more of a C++ discussion, wxWidgets, or both. But is there a wxWidgets way to say, take the file name from an wxFileDialog, and get it to a wxTreeCtrl? I started working on an observer pattern, or more specifically, a more generalized Publisher-Subscribe pattern and realized that I was heading towards multiple inheritance. I basically realized at the end of it I would be inheriting from my Observer base class/interface as well as the applicable wxMenu(i.e. File(wxMenu as Base class) derived). I would like a more generalized idea of what to do as it applies to C++, but I was also wondering if I can create an event in the File->OpenFile action, and place an EventHandler in the control I want to manage it(Almost like throwing an exception and letting someone else handle it). Any help would greatly be appreciated.
Would the solution potentially be where I first have my Observer inherit from wxObject(or looking at the hierarchy tree, wxTopLevelWindow), and then I have my all my current top-level classes inherit from Observer? Kind of like how wxWidgets has this crazy long chain of inheritance?
I also want to try to avoid friend functionality to maintain encapsulation.
EDIT: I'm actually wondering if a class template would be an option here. http://imgur.com/jTVH1wf
Would the solution potentially be where I first have my Observer inherit from wxObject(or looking at the hierarchy tree, wxTopLevelWindow), and then I have my all my current top-level classes inherit from Observer? Kind of like how wxWidgets has this crazy long chain of inheritance?
I also want to try to avoid friend functionality to maintain encapsulation.
EDIT: I'm actually wondering if a class template would be an option here. http://imgur.com/jTVH1wf
Re: Passing data between controls
it is probably easiest to simply have an event handler in the control that needs the event,
then in the file->open function use something like
wxcommand event
http://docs.wxwidgets.org/trunk/classwx ... abd913d450
creating a custom event
https://wiki.wxwidgets.org/Custom_Event ... xEventType
then in the file->open function use something like
Code: Select all
wxCommandEvent* evt = new wxCommandEvt(MY_CUSTOM_EVENT_TAG);
evt->SetString(fileDialogResult);
controlThatNeedsTheEvent->GetEventHandler()->QueueEvent(evt);
http://docs.wxwidgets.org/trunk/classwx ... abd913d450
creating a custom event
https://wiki.wxwidgets.org/Custom_Event ... xEventType
-
- Experienced Solver
- Posts: 52
- Joined: Thu Dec 08, 2016 2:42 pm
Re: Passing data between controls
Thanks. I attempted multiple inheritance and got close but I think I had conflicts with wxWidgets managed memory cleanup, so custom events seem to be the best option for anyone that attempt this.
EDIT: Well after looking at the links you sent me I think I'm still at the exact same situation as before. For me to queue an event I need a reference/pointer to the object to handle the event. So doesn't that leave me in the same situation where I have to have everything holding a ptr to each other? So every object I create will have to include pointers to every control it may queue an event for. That's why I went down the path of an observer to maintain encapsulation.
In the Custom Events link you sent it said one one line for posting your event....
What's that ...or class. part? For both wxQueueEvent and wxPostEvent I only see declarations involving a pointer to whatever derived class(of PostEvent or QueueEvent) you're using. Were there at some point some macros that allowed you to pass a class name that let you link your event to a handler in that class, but were taken out at some point? I'm using 3.0.2.
At this rate I guess I could organize my GUI into three main components/classes that all contain references/ptrs to each other. So then top class A can queue up an event for top class B, then top class B would handle the event, and queue up another event for one of it's component classes? Or is this the point where one might find it useful to derive from wxCommandEvent?
EDIT: Well after looking at the links you sent me I think I'm still at the exact same situation as before. For me to queue an event I need a reference/pointer to the object to handle the event. So doesn't that leave me in the same situation where I have to have everything holding a ptr to each other? So every object I create will have to include pointers to every control it may queue an event for. That's why I went down the path of an observer to maintain encapsulation.
In the Custom Events link you sent it said one one line for posting your event....
Code: Select all
wxPostEvent(pBar, event); // or to a different instance or class
At this rate I guess I could organize my GUI into three main components/classes that all contain references/ptrs to each other. So then top class A can queue up an event for top class B, then top class B would handle the event, and queue up another event for one of it's component classes? Or is this the point where one might find it useful to derive from wxCommandEvent?
-
- Experienced Solver
- Posts: 52
- Joined: Thu Dec 08, 2016 2:42 pm
Re: Passing data between controls
Okay, so this is a little sad that this wasn't the first thing I tried, but this is an acceptable option right? It works but I just want to make sure there are no hidden gotchas.
For wxFileDialog it works because it has the GetPaths(wxArrayString) method that allows me to get the data out of it. Just curious about packaging up data for say, a custom control. https://wiki.wxwidgets.org/Beech:Using_custom_dialogs looks like what I need.
Code: Select all
BEGIN_EVENT_TABLE( AnyControl, wxSomeControl)
....
EVT_MENU( ID_ANY, SomeOtherControl::DoSomething)
....
END_EVENT_TABLE()
Re: Passing data between controls
for linking events to other classes, I would prefer Bind()
http://docs.wxwidgets.org/3.1/overview_ ... vents_bind
keep scrolling on that page to see how events propogate.
http://docs.wxwidgets.org/3.1/overview_ ... vents_bind
keep scrolling on that page to see how events propogate.
Re: Passing data between controls
I think you're overcomplicating things. I've never had to jump to those hoops, especially when dealing with the result from a file dialog. Can you give a practical example of what you're trying to do?
Use the source, Luke!
-
- Experienced Solver
- Posts: 52
- Joined: Thu Dec 08, 2016 2:42 pm
Re: Passing data between controls
Oh that's my M.O.doublemax wrote:I think you're overcomplicating things. I've never had to jump to those hoops, especially when dealing with the result from a file dialog. Can you give a practical example of what you're trying to do?
I'm just wanting to make sure I'm doing it correctly, and that would be getting messages/data from one object to another. So I have all my menus at top and I just want to make sure I am getting the results of interacting with those menus to the consuming controls correctly and uniformly.
-
- Experienced Solver
- Posts: 52
- Joined: Thu Dec 08, 2016 2:42 pm
Re: Passing data between controls
Bind is just the most up to date to dynamically link right? Statically linking is still valid? I just kind of like having the event tables uniformly at the top of my source files.coderrc wrote:for linking events to other classes, I would prefer Bind()
http://docs.wxwidgets.org/3.1/overview_ ... vents_bind
keep scrolling on that page to see how events propogate.
Re: Passing data between controls
There is a nice tutorial "Widgets communicate" at the end of page: http://zetcode.com/gui/wxwidgets/firstprograms/
-
- Experienced Solver
- Posts: 52
- Joined: Thu Dec 08, 2016 2:42 pm
Re: Passing data between controls
I saw that awhile ago, I figured I would just end up having everything pointing to everything and I'd much rather not do that.Serge_N wrote:There is a nice tutorial "Widgets communicate" at the end of page: http://zetcode.com/gui/wxwidgets/firstprograms/
I got this working though mostly.
Code: Select all
Bind(wxEVT_COMMAND_MENU_SELECTED, wxCommandEventHandler(AuiManager::OnFileOpen), this, ID_FOPEN);
Re: Passing data between controls
Hi,
This is the old syntax with Connect().
The Bind line should be:
And you can of course inter-mix static and dynamic event tables.
Thank you.
This is the old syntax with Connect().
The Bind line should be:
Code: Select all
Bind( wxEVT_COMMAND_MENU_SELECTED, &AuiManager::OnFileOpen, this, ID_FOPEN );
Thank you.