PDA

View Full Version : Question about multiples forms.



Bruno1993
24th June 2015, 02:18
Hello, i have a problem in my application.
I'm programming an app with several forms; for example:
i have the class "MainWindow" and "Find_Patient" so i have this files:
MainWindow.cpp
MainWindow.h
MainWindow.ui
Find_Patient.cpp
Find_Patient.h
Find_Patient.ui.

Suppose that in MainWindow i have a button, and when i click that button, Find_Patient shows up and MainWindow dissapears.
so this is the code for that:



void MainWindow::on_open_find_patient_clicked()
{
Find_Patient *go = new Find_Patient
go->show(); //this is going bring up Find_Patient
this.hide(); //this is going to hide MainWindow
}


and this is the code for the button on Find_Patient.cpp to back to MainWindow



void Find_Patient::on_open_MainWindow_clicked()
{
MainWindow *back = new MainWindow
back->show(); //this is going bring up MainWindow
this.hide(); //this is going to hide Find_Patient
}

This code is totally functional, but it is not efficient.

Here's my problem:

Every time that i will click one of those buttons, the code will create a new object of type MainWindow or Find_Patient.
Doing this, it will ocupate a peace of memory every time that i click that button, because the previous object is hidden, not destroyed.
So my question is how i pass through forms?
This is the right way for doing it? or there is another way?.

i'm thinking on doing this:


MainWindow *back = new MainWindow
back->show(); //this is going bring up MainWindow
this.deleteLater(); //this is going to delete Find_Patient

but i don't know if this is correct.

Sorry my bad english, im spanish.

Thanks!

anda_skoa
24th June 2015, 09:54
1) define a signal in Find_Patient
2) connect it to the main window's show() slot after creating Find_Patient
3) emit the signal when you are closing Find_Patient

You can either make Find_Patient delete itself on close (e.g. setting the WA_DeleteOnClose widget attribute) or keeping the instance around and just showing it again when necessary.

Cheers,
_

Bruno1993
24th June 2015, 14:08
Suppose that i want to keep one instance around and i want to show it or hide it many times... And i want to do that from different clases. Is that possible?

anda_skoa
24th June 2015, 16:38
Sure.
You can either signal the main window that it should show the dialog or make the dialog instance accessible from elsewhere.

Cheers,
_