Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Special Interest Groups
  3. C++ Gurus
  4. QObject-inherited classes' virtual destructor method
QtWS25 Last Chance

QObject-inherited classes' virtual destructor method

Scheduled Pinned Locked Moved Solved C++ Gurus
8 Posts 4 Posters 596 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.
  • J Offline
    J Offline
    JonB
    wrote on 31 Oct 2024, 13:49 last edited by
    #1

    When you create a QObject-based class (e.g. a QWidget-derived class) in Creator it puts in an empty virtual destructor method for you, like:

    MyWidget::~MyWidget() {}
    

    I know I would need this if I add e.g. my own member variables (without QObject-parentage) which need destructing. So it may be a nice convenience for the user. Personally I like "if I don't need/use it, I don't define it", so I have one less thing to scroll through.

    AFAICS, I get no ill effects if I delete the empty destructor and my class does not have one. Is that right? So long as I don't actually have anything inside it is there any reason why it should nonetheless be defined for e.g. QObject-inherited classes?

    C 1 Reply Last reply 31 Oct 2024, 14:09
    0
    • J JonB
      31 Oct 2024, 13:49

      When you create a QObject-based class (e.g. a QWidget-derived class) in Creator it puts in an empty virtual destructor method for you, like:

      MyWidget::~MyWidget() {}
      

      I know I would need this if I add e.g. my own member variables (without QObject-parentage) which need destructing. So it may be a nice convenience for the user. Personally I like "if I don't need/use it, I don't define it", so I have one less thing to scroll through.

      AFAICS, I get no ill effects if I delete the empty destructor and my class does not have one. Is that right? So long as I don't actually have anything inside it is there any reason why it should nonetheless be defined for e.g. QObject-inherited classes?

      C Online
      C Online
      Christian Ehrlicher
      Lifetime Qt Champion
      wrote on 31 Oct 2024, 14:09 last edited by Christian Ehrlicher
      #4

      @JonB said in QObject-inherited classes' virtual destructor method:

      it is there any reason why it should nonetheless be defined for e.g. QObject-inherited classes?

      No, it's imo just added as a convenience as you normally want to clean things up in the dtor.

      Whether or not one ought type in override on a virtual destructor I agree is moot

      The Qt coding guide imo says it's useless to add 'override' here and therefore not allowed.

      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
      1
      • J Online
        J Online
        J.Hilk
        Moderators
        wrote on 31 Oct 2024, 14:01 last edited by
        #2

        I would consider it bad practice. At its core every from QObject derived class has a virtual destructor.
        So MyWidget::~MyWidget() {} overrides a destructor, but the macro clearly doesn't mark it as override.


        Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


        Q: What's that?
        A: It's blue light.
        Q: What does it do?
        A: It turns blue.

        J 1 Reply Last reply 31 Oct 2024, 14:08
        0
        • J J.Hilk
          31 Oct 2024, 14:01

          I would consider it bad practice. At its core every from QObject derived class has a virtual destructor.
          So MyWidget::~MyWidget() {} overrides a destructor, but the macro clearly doesn't mark it as override.

          J Offline
          J Offline
          JonB
          wrote on 31 Oct 2024, 14:08 last edited by JonB
          #3

          @J-Hilk
          Hi. Whether or not one ought type in override on a virtual destructor I agree is moot, but to be clear that is not my question at all. My question is: do you need/ought you define any ~MyWidget() destructor (with or without virtual and/or override) which is going to have an empty body ({})? I know it does no harm, and might be useful as a placeholder/reminder if you are going to add any code into it. But if you are not, and it's going to be empty, is there any good reason in C++ or for a QObject-derived class that you really should have it?

          J 1 Reply Last reply 31 Oct 2024, 14:10
          0
          • J JonB
            31 Oct 2024, 13:49

            When you create a QObject-based class (e.g. a QWidget-derived class) in Creator it puts in an empty virtual destructor method for you, like:

            MyWidget::~MyWidget() {}
            

            I know I would need this if I add e.g. my own member variables (without QObject-parentage) which need destructing. So it may be a nice convenience for the user. Personally I like "if I don't need/use it, I don't define it", so I have one less thing to scroll through.

            AFAICS, I get no ill effects if I delete the empty destructor and my class does not have one. Is that right? So long as I don't actually have anything inside it is there any reason why it should nonetheless be defined for e.g. QObject-inherited classes?

            C Online
            C Online
            Christian Ehrlicher
            Lifetime Qt Champion
            wrote on 31 Oct 2024, 14:09 last edited by Christian Ehrlicher
            #4

            @JonB said in QObject-inherited classes' virtual destructor method:

            it is there any reason why it should nonetheless be defined for e.g. QObject-inherited classes?

            No, it's imo just added as a convenience as you normally want to clean things up in the dtor.

            Whether or not one ought type in override on a virtual destructor I agree is moot

            The Qt coding guide imo says it's useless to add 'override' here and therefore not allowed.

            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
            1
            • J JonB
              31 Oct 2024, 14:08

              @J-Hilk
              Hi. Whether or not one ought type in override on a virtual destructor I agree is moot, but to be clear that is not my question at all. My question is: do you need/ought you define any ~MyWidget() destructor (with or without virtual and/or override) which is going to have an empty body ({})? I know it does no harm, and might be useful as a placeholder/reminder if you are going to add any code into it. But if you are not, and it's going to be empty, is there any good reason in C++ or for a QObject-derived class that you really should have it?

              J Online
              J Online
              J.Hilk
              Moderators
              wrote on 31 Oct 2024, 14:10 last edited by
              #5

              @JonB its probably there to delete the UI-pointer when you decide to create a class with .ui designer extension


              Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


              Q: What's that?
              A: It's blue light.
              Q: What does it do?
              A: It turns blue.

              1 Reply Last reply
              1
              • J JonB has marked this topic as solved on 31 Oct 2024, 14:13
              • T Offline
                T Offline
                TomZ
                wrote on 1 Nov 2024, 10:25 last edited by
                #6

                The destructor gets added by the compiler if you don't have it there as text. Much like an empty constructor on a struct or class.

                It will be there in the compiled result, but I agree it makes no sense for Creator to have that there.

                I do notice that if I inherit QObject, the wizard in QtCreator doesn't create any destructor.
                Maybe at some point the qwidget template had something in the destructor ?
                I haven't worked with widgets for a long time, not sure.

                J 1 Reply Last reply 1 Nov 2024, 10:58
                0
                • T TomZ
                  1 Nov 2024, 10:25

                  The destructor gets added by the compiler if you don't have it there as text. Much like an empty constructor on a struct or class.

                  It will be there in the compiled result, but I agree it makes no sense for Creator to have that there.

                  I do notice that if I inherit QObject, the wizard in QtCreator doesn't create any destructor.
                  Maybe at some point the qwidget template had something in the destructor ?
                  I haven't worked with widgets for a long time, not sure.

                  J Online
                  J Online
                  J.Hilk
                  Moderators
                  wrote on 1 Nov 2024, 10:58 last edited by
                  #7

                  @TomZ

                  like I said, it's there in case you select a designer form class in the add new option:

                  #ifndef TESTFORM_H
                  #define TESTFORM_H
                  
                  #include <QWidget>
                  
                  namespace Ui {
                  class TestForm;
                  }
                  
                  class TestForm : public QWidget
                  {
                      Q_OBJECT
                  
                  public:
                      explicit TestForm(QWidget *parent = nullptr);
                      ~TestForm();
                  
                  protected:
                      void changeEvent(QEvent *e);
                  
                  private:
                      Ui::TestForm *ui;
                  };
                  
                  #endif // TESTFORM_H
                  
                  
                  TestForm::TestForm(QWidget *parent)
                      : QWidget(parent)
                      , ui(new Ui::TestForm)
                  {
                      ui->setupUi(this);
                  }
                  
                  TestForm::~TestForm()
                  {
                      delete ui;
                  }
                  
                  void TestForm::changeEvent(QEvent *e)
                  {
                      QWidget::changeEvent(e);
                      switch (e->type()) {
                      case QEvent::LanguageChange:
                          ui->retranslateUi(this);
                          break;
                      default:
                          break;
                      }
                  }
                  

                  Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


                  Q: What's that?
                  A: It's blue light.
                  Q: What does it do?
                  A: It turns blue.

                  J 1 Reply Last reply 1 Nov 2024, 11:31
                  1
                  • J J.Hilk
                    1 Nov 2024, 10:58

                    @TomZ

                    like I said, it's there in case you select a designer form class in the add new option:

                    #ifndef TESTFORM_H
                    #define TESTFORM_H
                    
                    #include <QWidget>
                    
                    namespace Ui {
                    class TestForm;
                    }
                    
                    class TestForm : public QWidget
                    {
                        Q_OBJECT
                    
                    public:
                        explicit TestForm(QWidget *parent = nullptr);
                        ~TestForm();
                    
                    protected:
                        void changeEvent(QEvent *e);
                    
                    private:
                        Ui::TestForm *ui;
                    };
                    
                    #endif // TESTFORM_H
                    
                    
                    TestForm::TestForm(QWidget *parent)
                        : QWidget(parent)
                        , ui(new Ui::TestForm)
                    {
                        ui->setupUi(this);
                    }
                    
                    TestForm::~TestForm()
                    {
                        delete ui;
                    }
                    
                    void TestForm::changeEvent(QEvent *e)
                    {
                        QWidget::changeEvent(e);
                        switch (e->type()) {
                        case QEvent::LanguageChange:
                            ui->retranslateUi(this);
                            break;
                        default:
                            break;
                        }
                    }
                    
                    J Offline
                    J Offline
                    JonB
                    wrote on 1 Nov 2024, 11:31 last edited by
                    #8

                    @J-Hilk
                    Yes, as you say, it comes from UI app. I often pick the UI template for a test project so I get the QApplication and necessary Makefile/cmake stuff but then uncheck the "create .ui file" box as I don't want that and create widgets in code. Which leaves me with the empty destructor code. I am now happy that I don't need that unless I want it.

                    1 Reply Last reply
                    0

                    8/8

                    1 Nov 2024, 11:31

                    • Login

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