Hook from main.cpp

Good Day folks, firstly let me apologise I’m very much a new comer to programming, It’s something that’s sparked an interest since the first Lockdown and I’ve gruadually been teaching myself in-between work.

What I’m looking for is most likely an easy fix and I’d appreciate any responses (even snarky ones).

Within my Juce application I have a Main.cpp file and a MainComponent cpp file, I’m looking at getting a playlist item serialised when the application is closed, now the function call I see can go into closeButtonPressed() but I’m unsure how to hook a function that’s in MainComponent, juce should in theory have instantiated a version of MainComponent whereby I can then trigger my function, if I change the function to a static function I could then trigger what is needed but the serialised items is not accessible there.

Pardon my poor explanation and if needed I can try and explain better.

Kind regards,


The Projucer generates code like this to create the content component in the main window constructor, so the pointer to it isn’t cleanly accessible elsewhere :

setContentOwned (new MainContentComponent(), true);

You can change that to something like :

// add as a member variable of the MainWindow class :
// MainContentComponent* contentComponent = nullptr;

// in the constructor
setContentOwned (contentComponent = new MainContentComponent(), true);

That way, the content component becomes accessible for the other methods of the MainWindow class.

1 Like

@xenakios that’s brilliant, I’ve implemented it and it works beautifully.

Thank you!

kind regards,