addAndMakeVisible concepts


#1

Hi,

i've been trying for a long time now to understand and make things on the right way with components and how to show them and hide, but i am always finding problems using them.

I'm still a newby in JUCE, but since i found it, i discovered that it was exactly what i looked for years.

I'm not in the industry of Sounds, Midi and other great things that can be done with JUCE, but more in the industry of normal applications and the most important is with JUCE i can make one source to work in many platforms.

I tried some times already to post my dificulties but always have some problems to explain. But i need to try again, since I'm trying to build something and learn more and more each time.

I have one application that it will have database access.

This application have a main screen, that have 5 buttons.

This buttons, when pressed, will open a identification window that will ask for the id and password of the user.

After the authentication, and in case of success, will show another page, and this new page can show others thru buttons or functions they can have.

From this pages that we have after login, i can always go back to the main screen.

This is a father and son connection type, where the father is the mainwindow, and the son is the login page.

After we do login, i can close the login page, and from the father open the son that will be the functions available after identification.

Right now what i am doing is this way:

- I have a main class that will open the MainWindow, this main Window is of type DocumentWindow and i declare inside:

setContentOwned (new MainContentComponent(), true);

My MainContentComponent is declared as a Component:

Component* showMainPage() {

return new MainContentComponent();

}

In my main, i have all that is needed with buttons, listeners to the buttons, all defined as ScoopedPointers, that are destroyed when the MainContentComponent is destroyed.

On the main, i call the login component this way:

addAndMakeVisible (showLoginPage());

And showLoginPage is declared as:

Component* showLoginPage();

My LoginPage component is well declared as a component. And declared as modal this way:

Component::enterModalState(true);

All works well showing the login page, and closing the login page, when i want to close it, i am doing a:

Component::setVisible(false);

That hides my login page, and it's showned again when i press any button in the mainwindow.

Now my problem is, if the user authenticates well, i need to show another page (component), and when i try to do this, always crashes.

The only way i have to call it is directly from the login component, but my new page, is inside my login component, that does not occupy the full screen.

So my big issue is, how do i open and close this father and son components. How do i change from one to another. And how do i free the component i am not using anymore ?

Hope i had explained well.

Paulo

 

 


#2

I want to encourage you to keep asking questions and to keep experimenting with JUCE! However, it looks to me like some of the things you are asking about are more C++ problems than JUCE problems.

For example, this doesn't look like a good idea to me:

Component* showMainPage() {
return new MainContentComponent();

}

Can you find ANY other way to do this? This is dangerous for so many reasons:

1) jules (the real expert) says so:

http://www.juce.com/documentation/coding-standards

Read what he says about "Object Lifetime and Ownership" (item 2): "Do not use 'new' unless there's no alternative."

2) It is easy to misuse showMainPage(). What if you were to say this:

void MyClass::foo()
{
    ...
    // display the main page
    showMainPage();   // XXX oops! bad idea! not what you meant to do!
}

That code would create a memory leak. Do you know why?

3) Your function naming is confusing; showMainPage() doesn't actually show anything!

4) By returning a pointer to an object created by the new operator, you are passing the responsibility of managing that pointer back to the caller. Understand the risk of doing this before writing functions like this.

When you say this....

addAndMakeVisible (showLoginPage());

Are you aware that addAndMakeVisible() does not take ownership of the Component* that is returned by showLoginPage()? Again, you have created a dangling pointer (most likely). Can you see why this will probably result in a memory leak?

See if you can find the answers to each of my questions. These C++ concepts are important to understand. Read books, spend lots of time on StackOverflow.com, do whatever it takes. Once you understand some of the basic C++ ideas, JUCE will start to make a lot more sense.

Check out the JUCE demo file WindowsDemo.cpp. It has examples of how to show different types of windows.


#3

Hi Matty,

you are totally right on your observations.

I'm going to restart from zero (well, not zero because i already made a lots of things that can be used), but to redraw the way i was doing things.

My mistake was that i forgot the way that should be done and have done it following the JuceDemo and get a bad understanding of that.

Not because there is any error on JuceDemo, there is not, it's well done and a perfect example of the capabilities JUCE, but i tried to understand it and applying it to my needs and i have gone in a wrong way.

Just hope i can now restart well my project.

Will also read all that you suggested and learn more about C++.

I'm always learning, it's a non stop process of growing that never stops.

Thanks again,

Paulo