Do you need to delete Designer generated UI in the destructor?
-
wrote on 30 May 2022, 05:26 last edited by
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? }
-
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? }
Lifetime Qt Championwrote on 30 May 2022, 05:27 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)
-
wrote on 30 May 2022, 05:31 last edited by
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.
-
You only need to delete the ui pointer - the created widgets (by setupUi()) have a proper parent.
wrote on 30 May 2022, 05:42 last edited by@Christian-Ehrlicher
Great, Thank you!
1/5