PDA

View Full Version : delete the buttons and label permenantly



iswaryasenthilkumar
20th November 2015, 10:19
i have two screen,
first screen contains 4 button and 1 label
second screen contains 4 button and 1 label
my app will start in first screen,,when i move to second screen first screen buttons and label should delete permenantly, so i used code

void Widget:: deactivefirstscreen()
{
delete button1;
delete button2;
"
"
delete label1;
}
i getting erro when command reached this method()
Error:

The program has unexpectedly finished.


please give me clarification for this:(
Thanks in advance:o

Lesiok
20th November 2015, 10:29
How this method is started ?

iswaryasenthilkumar
20th November 2015, 10:38
when i click next screen method,this method first call deactivatefirstscreen(),and activate the secondscreenmethod()

How this method is started ?

Lesiok
20th November 2015, 11:16
What does it mean "click next screen method". Is it one from this buttons ?

iswaryasenthilkumar
20th November 2015, 12:00
yes lesiok
button1()//if it clicked it moves to second screen method(),


void Widget::button1()
{
deactivatefirstscreen(); //it should delete the labels and button in first screen
activateaddscreen();//its new screen(second screen)
}

What does it mean "click next screen method". Is it one from this buttons ?

Lesiok
20th November 2015, 12:37
So what are you surprised? It is exactly as if the object caused its own destructor. Nothing with Qt but C++ abc.
Change delete buttonxxx for buttonxxx.deleteLater().

Radek
20th November 2015, 20:42
I guess you are committing "double deleting". Double deleting isn't tolerated in C++ and usually results in GP fault.

button1, label1 and others are (I guess) child windows of firstscreen. The firstscreen gets destroyed in the end and when it gets destroyed, it destroys its children. But the children are already destroyed by you yourself explicitly. Therefore crash. Never destroy child windows explicitly. Especially, if you have built the firstscreen by a UI script (that means, if you designed the firstscreen in the Designer and activated it using setupUi()), the button1 and others are child windows of firstscreen.

Next, if my guessing is correct, it depends whether firstscreen and addscreen are children of some "main window" or not. If they are, you cannot destroy them either (the same reason as above). hide() the firstscreen instead of deleting it. Do the same to the addscreen if you need and destroy the "main window" in the end (or let the program logic to destroy it). If firstscreen and addscreen areboth top level windows then you need to destroy firstscreen explicitly. Therefore, do not destroy button1, label1 and other windows, destroy firstscreen. You will also need to destroy addscreen later.

The question is, whether you should destroy some windows at all. You cannot "optimize" you app this way. Your windows use only a small portion of resources used by your app so that such "optimizing" is no optimizing. If you have some "main window" then do not destroy any object explicitly. Use hide() instead of delete and note that hiding a window hides all its child windows. When you quit the app, the main window gets destroyed and this triggers an avalanche of deletions of all subordinate windows. Without a main window, you need to destroy top level windows yourself (and disable automatic deletion of windows when you quit the app because it is not clear what should be deletedand what has been already deleted).