Main component resize grief


#1

First, my apologies for what is probably going to be a dumb question but I'm utterly stumped :/

As I understand the documentation, I should be able to add code to the parent component's resized() callback to resize and reposition its child components.  However, the app crashes when I attempt this, even with the basic app assembled by IntroJucer.

As an example, below are the relevant bits from the IntroJucer constructed app.  The only I change I made to that boilerplate was to add a label component to the MainComponnent.h file.  The setBounds() call in the resized() function is a bit silly because it just repeats the exact same call as occurs in the main component's constructor.  Ideally, I'd make a call to reposition the label according to its parent's new size but even this simple call fails.  If I comment out the line in resized(), the app works fine.  So, what am I missing?!?!?

Thank you in advance for any response!  BTW I'm using VS2010 on Windows 7 64bit if that matters.

/*

Main.cpp

<snip> ... just the constructor bit

*/
    MainWindow()  : DocumentWindow ("MainWindow",
                                    Colours::lightgrey,
                                    DocumentWindow::allButtons)
    {
        setContentOwned (new MainContentComponent(), true);

        centreWithSize (getWidth(), getHeight());
        setVisible (true);
    }

/*

MainComponent.cpp

<snip> ... just the constructor and resized() bits

*/

MainContentComponent::MainContentComponent()
{
    setSize (500, 400);

    label = new Label ("label", "eat at joe's");
    label->setBounds(100, 100, 120, 20);
    addAndMakeVisible (label);
}

void MainContentComponent::resized()
{
    label->setBounds(100, 100, 120, 20);
}


#2

setSize will call resize() before you've created your label, so surely you have a null/uninitialised pointer crash there?


#3

Yop, that's it - of course, as if you didn't know LOL

What I was refusing to consider was the idea that the parent component could/should be sized *after* its children were dimensioned, esp. if one of the children needs to know the parent dimensions in order to center itself.  Simple fix was to not size the label in the parent constructor at all but rather set it in the resized() callback.

Thanks for pointing me in the right direction, so to speak...  :)