Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Do you need to delete Designer generated UI in the destructor?
Forum Update on Monday, May 27th 2025

Do you need to delete Designer generated UI in the destructor?

Scheduled Pinned Locked Moved Solved General and Desktop
designercleanupdelete
5 Posts 2 Posters 1.2k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • C Offline
    C Offline
    Curtwagner1984
    wrote on 30 May 2022, 05:26 last edited by
    #1

    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? 
    }
    
    C 1 Reply Last reply 30 May 2022, 05:27
    0
    • C Curtwagner1984
      30 May 2022, 05:26

      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? 
      }
      
      C Online
      C Online
      Christian Ehrlicher
      Lifetime Qt Champion
      wrote on 30 May 2022, 05:27 last edited by Christian Ehrlicher
      #2

      @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)

      Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
      Visit the Qt Academy at https://academy.qt.io/catalog

      1 Reply Last reply
      2
      • C Offline
        C Offline
        Curtwagner1984
        wrote on 30 May 2022, 05:31 last edited by
        #3

        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 to deleteLater() them individually?

        1 Reply Last reply
        0
        • C Online
          C Online
          Christian Ehrlicher
          Lifetime Qt Champion
          wrote on 30 May 2022, 05:39 last edited by
          #4

          You only need to delete the ui pointer - the created widgets (by setupUi()) have a proper parent.

          Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
          Visit the Qt Academy at https://academy.qt.io/catalog

          C 1 Reply Last reply 30 May 2022, 05:42
          2
          • C Christian Ehrlicher
            30 May 2022, 05:39

            You only need to delete the ui pointer - the created widgets (by setupUi()) have a proper parent.

            C Offline
            C Offline
            Curtwagner1984
            wrote on 30 May 2022, 05:42 last edited by
            #5

            @Christian-Ehrlicher
            Great, Thank you!

            1 Reply Last reply
            0

            2/5

            30 May 2022, 05:27

            • Login

            • Login or register to search.
            2 out of 5
            • First post
              2/5
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • Users
            • Groups
            • Search
            • Get Qt Extensions
            • Unsolved