Do you need to delete Designer generated UI in the destructor?
-
Hello,
If I have a pointer to an UI generated by the designer, do I need to delete it myself?
Most Qt classes can be proved with a parent argument and when the parent is deleted the child will be deleted as well.
Does this happen automatically to objects generated by the designer?For example:
MainWindow.h
... public: explicit MainWindow(QWidget *parent = 0); ~MainWindow(); private: Ui::MainWindow* ui;
MainWindow.cpp
MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent), ui(new Ui::MainWindow) { ... } MainWindow::~MainWindow(){ ... delete this->ui; //<- Is this needed? }
-
Christian Ehrlicher Lifetime Qt Championreplied to Curtwagner1984 on last edited by Christian Ehrlicher
@Curtwagner1984 said in Do you need to delete Designer generated UI in the destructor?:
delete this->ui; //<- Is this needed?
Yes since it's a raw pointer (and not derived from QObject where the parent-child relationship will delete it automatically)
-
Ok, thanks!
So do I need to traverse the ui widget tree and do
deleteLater()
on them before deleting the ui pointer?
Or is calling delete on the ui pointer will delete all it's children and there is no need todeleteLater()
them individually? -
You only need to delete the ui pointer - the created widgets (by setupUi()) have a proper parent.
-
@Christian-Ehrlicher
Great, Thank you!