What is the correct way to open and close window/dialog?
I'm trying to develop a new program. The work flow looks like this:
Login --> Dashboard (Window with menus) --> Module 1
--> Module 2
--> Module 3
--> Module XXX
So, to open Dashboard from Login (a Dialog), I use
Dashboard 开发者_如何学C*d = new Dashboard();
d->show();
close();
In Dashboard, I use these codes to reopen the Login if the user closes the Window (by clicking the 'X')
closeEvent(QCloseEvent *)
{
Login *login = new Login();
login->show();
}
With a Task Manager opened, I ran the program and monitor the memory usage. After clicking open Dashboard from Login and closing Dashboard to return to Login, I noticed that the memory keeps increasing about 500 KB. It can goes up to 20 MB from 12 MB of memory usage by just opening and closing the window/dialog.
So, what did I do wrong here ? I need to know it before I continue developing those modules which will definitely eat more memory with my programming. Thanks in advance.
One reason might be you are creating the Login
widget again and again without deleting it.
I believe your Login is a QWidget
, Dashboard is a QDialog
. Instead of close()
, just hide the Login widget by hide()
or setvisible(false)
.
In the closeEvent()
of the DashBoard give
done(someIntValue);
While accessing DashBoard, instead of show() give
int returnValue = d->exec();
if the returnValue
is someIntValue
, show()
the Login Widget.
This way you are avoiding the creation of the Login widget multiple times. But a lot other factors might be responsible for your memory usage, which could be found out only by going through the entire code.
Edit:
Since your Dashboard is a QMainWindow you can't call done(someIntValue);
Instead try connecting the destroyed( QObject * obj = 0 )
signal of the DashBoard and in the corresponding slot show()
your Login.. Of course you should have the instance of the DashBoard in the Login Dialog and the connection between the above signal and slot should be made in the Login.
Hope it helps.
精彩评论