Hi,
I’m having quite a bit of trouble getting the GUI for my standalone audio app to work. Trying to keep with good practice, I’m separating my GUI from my MainComponent, but here is where the troubles arise. I’ve tried looking through the tutorials on GUI and parent/child, and also searched this forum, along with external forums, but I can’t wrap my head around it…
Drawing the components is fine, but everything falls apart when I want to use listeners. I tried using the lambda functions, but for me it seemed impossible, but if anyone has suggestion with lambdas I would prefer that solution.
Anyways, my code triggers a breakpoint in juce_Component.cpp
with a comment suggesting: “adding a component to itself!?”. I thought I’d done it the correct way with forward declaration, but it seems not. My code looks like this:
// =============================================================================
// MainComponent.h
#pragma once
// includes... (NOT appGUI.h)
// Forward declaration
namespace myApp // I'm using a namespace across several files, but not in MainComponent.h
{
class TabbedComp;
};
class MainComponent : public juce::Slider::Listener
{
public:
MainComponent();
~MainComponent() override;
// I needed to override these virtual functions of Slider::Listener
void sliderValueChanged(juce::Slider* slider) override;
void sliderDragStarted(juce::Slider*) override;
void sliderDragEnd(juce::Slider*) override;
// ...
private:
// ...
myApp::TabbedComp* gui;
};
// =============================================================================
// MainComponent.cpp
MainComponent::MainComponent()
{
gui = new anyMidi::TabbedComp(this);
addAndMakeVisible(gui);
}
void MainComponent::resized()
{
gui->setBounds(getLocalBounds().reduced(4));
}
void MainComponent::sliderValueChanged(juce::Slider* slider) {}
void MainComponent::sliderDragStarted(juce::Slider*) {}
void MainComponent::sliderDragEnded(juce::Slider*) {}
Then there is the App GUI:
// =============================================================================
// appGUI.h
#pragma once
#include <JuceHeader.h>
#include "MainComponent.h"
namespace myApp {
class TabbedComp : public juce::TabbedComponent
{
public:
TabbedComp(MainComponent* mc);
};
class AppSettingsPage : public juce::Component
{
public:
AppSettingsPage(juce::Slider::Listener* mc);
void resized() override;
juce::Slider mySlider;
};
}; // namespace myApp
// =============================================================================
// appGUI.cpp
#include "appGUI.h"
using namespace anyMidi;
TabbedComp::TabbedComp(MainComponent* mc) : TabbedComponent(juce::TabbedButtonBar::TabsAtTop)
{
addAndMakeVisible(this);
auto color = juce::Colour(0, 0, 0);
addTab("App Settings", color, new AppSettingsPage(mc), true);
}
AppSettingsPage::AppSettingsPage(juce::Slider::Listener* mc)
{
mySlider.addListener(mc);
}
Please help me understand how to do this properly! As I said before, I’d like to use lambdas, but couldn’t understand how I would acces the MainComponent callback functions I wanted them to trigger without having circular includes.