PDA

View Full Version : how to delete a widget



shivcena
12th November 2015, 05:30
ACtually im having 2 Qwidgets say "Widget" , "NewWidget". im calling "NewWidget" in "Widget" keypress event and viced versa,

void Widget::keyPressEvent(QKeyEvent *qv)
{
// i want to delete "Widget" here before i call "NewWidget"
NewWidget *newWidget = new NewWidget();
newWidget->setStyleSheet("background-color:black;");
newWidget->setGeometry(0,0,640,480);
newWidget->show();
}

I want to delete or destroy the "Widget" before calling "NewWidget"

prasad_N
12th November 2015, 05:45
Your trying to delete this object in its own function which is dangerous(object self destruction).

May be you can use this->deleteLater(); in this case.

But destroying self object is bad idea.

What is your requirement.?

shivcena
12th November 2015, 08:34
im running this executable in ARM based board which is having very less memory.Actually im using 4 widgets in my application,so if i delete the widget while calling another , i can save the memory thats the reason im looking for.

anda_skoa
12th November 2015, 10:16
As prasad_N said, use deleteLater() if you do it inside the widget.
If you need to delete the current widget before you create the new one, then you need to emit a signal, connect it to some slot using a Qt::QueuedConnection and in the slot first delete the current widget, then create the new one.

Cheers,
_