Global object of custom component doesn't work


#1

I have the 3 files for the application Main.cpp, GUI.h and GUI.cpp. Inside GUI.h and GUI.cpp I have all the classes for every gui component I have in my application. So far everything was great but now I need to access a child component (lets call this component statusbar) function from another child component (call this component imagepanel). So I decided to declare statusbar globally at the top of GUI.cpp (‘StatusBar statusbar;’) and then hopefully access statusbar’s functions from imagepanel. That didn’t work, the program failed on an assertion (‘jassert(Colours::white==0xffffffff);’) saying in comments something was not initialized correctly. Since that didn’t work I changed ‘StatusBar statusbar;’ to a pointer ‘StatusBar *statusbar;’ and then of course I did ‘statusbar = new StatusBar();’ inside main gui component’s constructor and changed every call to statusbar from ‘statusbar.’ to ‘statusbar->’. I thought everything should be good now but when I tried to run, it just crashed somewhere inside the ‘juce_Rectangle’ file. Please someone knows what’s wrong???


#2

Just sounds like basic C++ beginner misunderstandings, but very hard for people to help without you showing your code.

(But yes, definitely never declare a global or static Component, that’s really not a good idea!)


#3

I never declare globally but I feel that I don’t have any other option here. I mean what’s the best way to contact between different components?


#4

Pass pointers or references of the components between each other. Or preferably, consider designs where the components don’t need to directly know of each other. For example the Juce broadcaster/listener design is an example of the latter.


#5

I tried something like this:

       class ChildComp : public Component {
                public:
                      ChildComp(ParentComp& parent) {

                      }
       };

but it dosn’t work, it gives me error on constructor. “No instance of overloaded function matches the the specified type”


#6

does your header match your implementation?

if you google that error message, all of the answers are basically that.


#7

Okay, that was actually a stupid error. The class definition of child class was on the top of parent class definition, easily solved by adding class ParentComp; on top of child class in header file. However, I found a better(?) way to “connect” gui components. In every child component of the main gui component I added a private member objevt ‘GUI mainGUI;’ and a public function 'void setMainGUI(GUI mainGUI) { mainGUI = mainGUI; }’ and everytime I create a child instance inside the main gui component I pass itself on the setter function. I think this is a good solution, please correct me if I am wrong.

Btw, I just realized that when I resize the window of the application, I notice some flickering. It’s not smooth. Irrelevant I know, but any idea why?


#8

What are you doing in your resized() method(s)?


#9

I just resize everything according to the new size of the window. Nothing more.