This most certainly couldn’t be more trivial, but since I’m still a complete novice in programming and especially in C++, I’m still scratching my head about it.
I want to call a method on a parent component from within its child component. From my searches so far on this forum, the solution I read of the most is to pass a reference of the parent down to the child. Here’s what I got:
MainComponent.h
#pragma once
#include <JuceHeader.h>
#include "ChildComponent.h"
class MainComponent : public juce::Component
{
public:
MainComponent();
~MainComponent() override;
void paint (juce::Graphics&) override;
void resized() override;
void setLabelText(juce::String newText);
private:
juce::Label textLabel;
ChildComponent childComponent;
JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (MainComponent)
};
MainComponent.cpp
#include "MainComponent.h"
MainComponent::MainComponent()
: childComponent(*this)
{
setSize (600, 400);
addAndMakeVisible(textLabel);
textLabel.setText("Text goes here....", juce::dontSendNotification);
addAndMakeVisible(childComponent);
}
MainComponent::~MainComponent()
{
}
void MainComponent::paint (juce::Graphics& g)
{
g.fillAll (getLookAndFeel().findColour (juce::ResizableWindow::backgroundColourId));
}
void MainComponent::resized()
{
textLabel.setBounds(20, 20, getWidth(), 25);
childComponent.setBounds(0, 50, getWidth(), getHeight() - 50);
}
void MainComponent::setLabelText(juce::String newText)
{
textLabel.setText(newText, juce::dontSendNotification);
}
ChildComponent.h
#pragma once
#include <JuceHeader.h>
#include "MainComponent.h"
class ChildComponent : public juce::Component
{
public:
ChildComponent(MainComponent& mainRef);
~ChildComponent() override;
void paint (juce::Graphics&) override;
void resized() override;
private:
MainComponent& mainComponentRef;
juce::TextButton childButton;
JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (ChildComponent)
};
ChildComponent.cpp
#include <JuceHeader.h>
#include "ChildComponent.h"
ChildComponent::ChildComponent(MainComponent& mainRef)
: mainComponentRef(mainRef)
{
addAndMakeVisible(childButton);
childButton.setButtonText("Child Button");
childButton.onClick = [this]() {mainComponentRef.setLabelText("Updated Text"); };
}
ChildComponent::~ChildComponent()
{
}
void ChildComponent::paint (juce::Graphics& g)
{
g.fillAll (getLookAndFeel().findColour (juce::ResizableWindow::backgroundColourId));
g.setColour (juce::Colours::grey);
g.drawRect (getLocalBounds(), 1);
}
void ChildComponent::resized()
{
childButton.setBounds(10, 10, 100, 30);
}
So overall extremely simple, just a Label
in the MainComponent and a TextButton
in the ChildComponent that’s supposed to call the setLabelText()
method to change the label’s text.
The problem is that once I try to compile this I get a multitude of errors (syntax errors, missing type errors, …). From what I’ve gathered from searching the web this might be a circular dependency issue, since I include ChildComponent.h
in the MainComponent header and the MainComponent.h
in the ChildComponent header?
I’ve also tried forward declaring class MainComponent;
in the child header instead of #include "MainComponent.h"
, but then the compiler complains about the use of undefined type ‘MainComponent’.
What am I doing wrong?
Thanks!