[SOLVED] Am I deleating these variables right?
-
just wondering if I was deallocating these variables properly:
' ' '
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
QWidget *window = new QWidget;QSplitter splitter; window->setWindowTitle("test"); checkbox *box = new checkbox; QHBoxLayout *mainLayout = new QHBoxLayout; splitter.addWidget(box); splitter.addWidget(window); window->setLayout(mainLayout); window->show(); return a.exec();
}
' ' ' -
Hi And welcome
As far as I know, the Layout will not delete objects
And since you NEW "window," i do not think it get deleted.
You do not need to new QWidget.
Can just be
QWidget window;
In which case it will be deleted when main ends.
Also if you use window for checkbox
checkbox *box = new checkbox(Window);
It will be owned by window and deleted with it. -
As far as I know, the Layout will not delete objects
The layout won't, but the Window will.
Also if you use window for checkbox
checkbox *box = new checkbox(Window);
It will be owned by window and deleted with it.A widget put in a layout which is then used in another widget has its parent reassigned. Therefore the QCheckBox will indeed be deleted at the time the Window is. I consider a good practice to set the parent anyway, I don't think it hurts.
-
@JohanSolo
Ahh, I did not know using a layout would reparent.So in this case, all he needs is
delete window; -
-
Thanks for the replies, i'll check out those docs
-
but if i were to, say, make a program in which the widgets are added to the layout in the class constructor, should I use the the splitter technique to destroy all the instances of the class that I made?
also if I need to delete window, should i just add it to splitter so that when splitter is deleted (it is a local variable) window will subsequently be deleted as well?
-
@manny said:
but if i were to, say, make a program in which the widgets are added to the layout in the class constructor, should I use the the splitter technique to destroy all the instances of the class that I made?
All the objects (deriving from QObject) you allocate and add to the layout of a widget will be destroyed when the widget itself is destroyed.
@manny said:
also if I need to delete window, should i just add it to splitter so that when splitter is deleted (it is a local variable) window will subsequently be deleted as well?
If I'm not mistaken, you can rely on QApplication::aboutToQuit() to destroy your widget:
connect( &a, SIGNAL( aboutToQuit() ), window, SLOT( deleteLater() ) );
See the doc for detailed information.
-
so basically if I make a new instance of a class like
' className object = new className;
object->show();it will be deleted once className is desetroyed
-
@manny said:
so basically if I make a new instance of a class like
' className object = new className;
object->show();it will be deleted once className is desetroyed
Not exactly: every QWidget destroys its children. The deleteLater trick allows to destroy the top-level widget when the application quits.
-
ok, i think i understand now. Thanks for the help, really appreciate it.
-
Although the answers given here were correct it is worth noting that the whole topic could have been avoided by not allocation the window on the heap. If the app has a single main widget it's simpler to just allocate it on the stack and don't fiddle with
aboutToQuit
:int main(int argc, char *argv[]) { QApplication a(argc, argv); QWidget window; window.setWindowTitle("test"); window.show(); return a.exec(); }
Also, if a widget is created on the heap and you want it deleted when it is closed, the simplest way is to set appropriate attribute:
QWidget* w= new QWidget(); w->setAttribute(Qt::WA_DeleteOnClose); //this will call deleteLater() when widget is closed. w->show();
-
thanks for the reply, will note that down