Accessing menu items outside of frame Topic is solved

If you are using the main C++ distribution of wxWidgets, Feel free to ask any question related to wxWidgets development here. This means questions regarding to C++ and wxWidgets, not compile problems.
Post Reply
dark.tempest360
In need of some credit
In need of some credit
Posts: 7
Joined: Fri Jun 15, 2018 4:24 pm

Accessing menu items outside of frame

Post by dark.tempest360 »

I am currently trying to access a check menu item initialized on the frame used by wxApp. How does one access it outside of the frame? Trying to store a pointer to the frame (or menubar or menu or menuitem) results in a segmentation fault whenever I access it again even if the pointer works upon storage.
User avatar
doublemax
Moderator
Moderator
Posts: 19115
Joined: Fri Apr 21, 2006 8:03 pm
Location: $FCE2

Re: Accessing menu items outside of frame

Post by doublemax »

That should work. Please show where and how you store the pointer and where and how you use it.
Use the source, Luke!
dark.tempest360
In need of some credit
In need of some credit
Posts: 7
Joined: Fri Jun 15, 2018 4:24 pm

Re: Accessing menu items outside of frame

Post by dark.tempest360 »

Code: Select all

class Control{
    MainFrame *frame;
    
    public:
    void s(MainFrame *menu);
};

void Control::s(MainFrame *menu){
    frame = menu;
    frame->GetMenuBar()->Check(ID_ModeChecked, false);
}

bool Control::checkItem(){
    return frame->GetMenuBar()->IsChecked(ID_ModeChecked);
}
this is part of the code I use. MainFrame is the subclass of wxFrame and s is called to store the wxFrame pointer. When I call checkItem() from another function, it causes a segmentation fault. (Ive tried almost the same thing with wxMenuBar, wxMenu, wxMenuItem) I must be doing something wrong with storing the wxFrame pointer
User avatar
doublemax
Moderator
Moderator
Posts: 19115
Joined: Fri Apr 21, 2006 8:03 pm
Location: $FCE2

Re: Accessing menu items outside of frame

Post by doublemax »

That doesn't look like real code, you probably stripped/rewrote a lot. There is nothing special about storing a wxFrame pointer, so there must be an error somewhere which is not in the code you posted.

Just for a test, replace:

Code: Select all

frame->GetMenuBar()->Check(ID_ModeChecked, false);
with:

Code: Select all

wxGetTopLevelParent(this)->GetMenuBar()->Check(ID_ModeChecked, false);
Use the source, Luke!
dark.tempest360
In need of some credit
In need of some credit
Posts: 7
Joined: Fri Jun 15, 2018 4:24 pm

Re: Accessing menu items outside of frame

Post by dark.tempest360 »

wxGetTopLevelParent worked and showed that im not storing the same pointer (strangely). Trying to store the pointer output of wxGetTopLevelParent results in another unusable pointer causing a segmentation fault. My code is really long at the moment so i stripped it down for the specific problem. I am trying to store settings on a common class not inheriting from any wxWidget class but is called by custom UI elements.

Id package the whole code into a zip file for compilation if it was more convenient to test with

EDIT: it seems that my wxFrame is not reliably giving out information... i will try working out why
Post Reply