GetParent() issue 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
nkwinder
Experienced Solver
Experienced Solver
Posts: 70
Joined: Sun Nov 23, 2008 2:32 pm

GetParent() issue

Post by nkwinder » Mon Dec 22, 2008 9:03 pm

hello all

i have a simple question, that i can't figure out.

i have two classes. MyFrame and MyCanvas.

MyFrame creates the MyCanvas object.

Code: Select all

MyFrame::MyFrame(const wxString& title, const wxSize& size, const wxPoint& pos)
        : wxFrame(NULL, wxID_ANY, title, pos, size)
{
    SetMinSize(wxSize(420,300));
    m_canvas = new MyCanvas( this, wxID_ANY, wxPoint(0,0), wxSize(-1,-1) );

    mbar=CreateToolBar(wxTB_TEXT );
    PopulateToolbar(mbar);
}

as you see i pass (this) in the first argument of the constructor of MyCanvas. So MyCanvas knows that its father is MyFrame.

Now the problem is, that i want to acces the mbar, (toolbar) from inside MyCanvas. But when i use

Code: Select all

GetParent()->mbar->GetToolState(wxID_ZOOM)
compiler says...'class wxWindow' has no member named 'mbar'. Why wxWindow?? MyCanvas parent should have been MyFrame not wxWindow.

Am i missing sth here?

thanks in advance

JimFairway
wxWorld Domination!
wxWorld Domination!
Posts: 1059
Joined: Sun Dec 30, 2007 6:40 pm
Location: Canada

Post by JimFairway » Mon Dec 22, 2008 10:24 pm

Hi,

It's because GetParent() returns a wxWindow which most classes derive from (including your MyFrame).

You should be able to cast the result, and access your frame as follows:

Code: Select all

MyFrame *myParent = (MyFrame *) GetParent();
myParent->mbar->GetToolState(wxID_ZOOM);

Hope that helps,

Jim
OS: Vista SP1, wxWidgets 2.8.7.

nkwinder
Experienced Solver
Experienced Solver
Posts: 70
Joined: Sun Nov 23, 2008 2:32 pm

Post by nkwinder » Mon Dec 22, 2008 10:42 pm

thanks!! that really helped!

Post Reply