Trying to understand Viewport

gui

#1

Playing around and created a trivial application that puts an instance of MusicPane in a Viewport and displays it. MusicPane inherits from Component and calls setSize(1000, 1000) in its constructor. This code works as expected:

MainComponent::MainComponent()
{
setSize (600, 400);
addAndMakeVisible(viewport);
addAndMakeVisible(musicPane);
musicPane.setSize(1000, 1000); // why is this necessary?
viewport.setViewedComponent(&musicPane, false);
viewport.setScrollOnDragEnabled(true);
viewport.setViewPosition(500, 500);
}

The MusicPane shows up and everything works fine. What puzzles me is that if I fail to call musicPane.setSize() in MainComponent(), the Viewport doesn’t show anything at all; I just see the MainComponent. How is calling musicPane.setSize() here different from calling setSize() in MusicPane’s constructor?

(Juce 5.3, if it matters.)


#2

You shouldn’t add musicPane as a child component of your MainComponent if you’re putting it in a Viewport (see the docs here). Remove the addAndMakeVisible(musicPane); line from your constructor.

(Also, if you surround your code blocks with three backticks ``` then it will be formatted correctly).


#3

Ah. Makes sense. Thanks.