MainComponent reference for MainComponent object

Hi,

I am designing a standalone JUCE application. I’ve got a problem with calling one MainComponent function, and I want to explain it on the easier example.

I create the new JUCE project for GUI application. In MainComponent class, I add the Label, make it visible on the app window, and write the function setLabelText(), which sets the text “Lalala” on this label.
Then I create the new class for GUI object called e.g. PressButton. This class contains only the 2-state TextButton (on/off). The object of this class is created in the MainComponent.h file, and its parameters are set in MainComponent constructor and MainComponent::resize() function. Of course the PressButton class also have the lambda function “Update Toggle state”. The MainComponent class and the PressButton class don’t inherit themself in any way, simply PressButton class is included into the MainComponent.h file, and in this file the object of this class is created.

My question is: How can I call the setLabelText() function (from MainComponent class) from the lambda function of the PressButton class? (so when I press the button, the text will appear). I try to include MainComponent.h and PressButton.h in several ways, but Visual Studio 2019 doesn’t compile the program. I know one trick - include the PressButton.h file in MainComponent.cpp file, and create the e.g. dynamic object of Press Button class for example in MainComponent constructor, but in the main source code I need the access to this object from any function of MainComponent (in this case - I’ve got this access only in MainComponent constructor). Any other ideas?

Thanks in advance,
Kamil

First, I think you are approaching it from the wrong direction, which is super easy with something like JUCE where you start creating GUI components, and then bind functionality to those GUI’s. This also leads to a tight coupling of GUI components. You might instead think about the underlying data, and how the GUI reflects the state of that and how it modifies it. So, in your case, the button changes something, and the label displays something related to that. If the label were to be alerted when ever that underlying data changed, the button could change it, and the label would update. ValueTrees are a great way to do this, as that is pretty much what they were built for. :slight_smile:

Maybe I explain it on the source code: The MainComponent class is the Main GUI class, but it also carries all of the processing functions. The database (variables) are stored into the structs, and I’ve got easy access to them using references. In MainComponent class I’ve got an object of ChordInput Class, which is rather an GUI object. It’s included into the MainComponent.h file, and created in this file. MainComponent class refers to this object when the configuration file is recalled (it sets all the GUI objects (ComboBoxes, TextButtons) according to the values stored in configuration file). Now I need that when I set the ComboBox of this object in some way, one Label of MainComponent class should set Text to empty. So I need to recall the setText() function of the MainComponent Label when I change the ComboBox item, which is an object of the ChordInputs class, which in fact is the object of MainComponent class. Can the ValueTrees help me on this?

Sorry I don’t have time to dig into the details of your description. But, whatever the model is, decoupling your GUI from your data is the correct thing to do, and ValueTrees are excellent for that. Hopefully someone else can give you more detailed help. :slight_smile:

1 Like