Reputation: 1010
I'd like to understand how to do a specific task. I was trying to set up event tables in wxWidgets and ran into an issue with their behaviour.
I set up some code in one class:
void TopRightPanel::OnSelChanged(wxTreeEvent& event)
{
wxTreeItemId item = event.GetItem();
TreeItemData* data = (TreeItemData *) GetItemData(item);
if(data != NULL)
{
particleSystemManager->SetSelectedParticleSystem(data->particleSystem);
}
}
This works fine and has the right values as expected. My problem with this though is that it's self contained and I want the class above it in the hierarchy to read the treeCtrl's action and make changes to all aspects of the U.I. So I tried this:
void Window::OnSelChanged(wxTreeEvent& event)
{
wxTreeItemId item = event.GetItem();
TreeItemData* data = (TreeItemData *) topRightPanel->GetItemData(item);//item.m_pItem.//m_MyTreeCtrl->GetItemData(itemId);*/
if(data != NULL)
{
particleSystemManager.SetSelectedParticleSystem(data->particleSystem);
}
}
Now I get an unhandled exception when topRightPanel->GetItemData(data) is called. The topRightPanel it uses doesn't seem to be updated and seems to be pointing to data before it's enstantuated in the class' constructor. Is there anyway I can get round this?
Edit:
I declare the event table like so:
#if USE_GENERIC_TREECTRL
BEGIN_EVENT_TABLE(TopRightPanel, wxGenericTreeCtrl)
#else
BEGIN_EVENT_TABLE(TopRightPanel, wxTreeCtrl)
#endif
EVT_TREE_SEL_CHANGED(TopRightPanel_Ctrl, Window::OnSelChanged)
END_EVENT_TABLE()
and I then declare the table in the header using DECLARE_EVENT_TABLE.
Upvotes: 1
Views: 1484
Reputation: 22678
You must use the same class name in the event table macros that you used in BEGIN_EVENT_TABLE
. IOW, your handler must be defined in Window
event table and not in TopRightPanel
event table. As tree events are wxCommandEvents
, they are propagated upwards to the parent so if Window
contains the tree control, this will work. Otherwise, e.g. if they are siblings, you would have to use Connect()
as indicated in another answer.
(Re)reading the event handling overview would be highly recommended.
Upvotes: 1
Reputation: 22678
You don't show how do you connect this event handler but the problem is almost surely there and not in the code you show. If you're using Connect()
, make sure that you pass the pointer to Window
object as its last argument, otherwise you end up calling a method of Window
class on TopRightPanel
object with the unsurprisingly catastrophic consequences.
If you're sure that you do call the method on the right object, then the only other explanation I see is that you don't initialize topRightPanel
before the first event of this type is generated. If this is the case, the simplest solution is to initialize the pointer to NULL
and set it correctly at the end of the initialization code. And, of course, check for the pointer being non-NULL in the event handler.
Upvotes: 0