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. How to properly close a widget
Forum Update on Monday, May 27th 2025

How to properly close a widget

Scheduled Pinned Locked Moved Unsolved General and Desktop
c++qt 5.9.5qwidgetclose
6 Posts 4 Posters 2.6k 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.
  • D Offline
    D Offline
    Daniel_Contro
    wrote on last edited by
    #1

    Hi to everyone,
    I'm developing an application that has several custom widgets dynamically added/removed and I was wondering which is the proper way to close them, keeping in mind that they contain data that should be saved. I read something in the Qt's documentation but I can't comprehend if I should define a slot e.g. onClose() in each widget connected to QWidget::close() or if I should override QWidget::closeEvent().

    Pl45m4P B 2 Replies Last reply
    0
    • D Daniel_Contro

      Hi to everyone,
      I'm developing an application that has several custom widgets dynamically added/removed and I was wondering which is the proper way to close them, keeping in mind that they contain data that should be saved. I read something in the Qt's documentation but I can't comprehend if I should define a slot e.g. onClose() in each widget connected to QWidget::close() or if I should override QWidget::closeEvent().

      Pl45m4P Offline
      Pl45m4P Offline
      Pl45m4
      wrote on last edited by
      #2

      @Daniel_Contro said in How to properly close a widget:

      override QWidget::closeEvent()

      This is one possible way.

      Depends on what QWidget you have and what data you are editing, you could make use of windowModified(click)


      If debugging is the process of removing software bugs, then programming must be the process of putting them in.

      ~E. W. Dijkstra

      1 Reply Last reply
      0
      • D Offline
        D Offline
        Daniel_Contro
        wrote on last edited by Daniel_Contro
        #3

        It's mostly text, but also the widgets order is important to me (it's a hierarchical data structure); I have my own Controller and Model where I should store the data.

        Should I override closeEvent() for every different custom widget that requires saving his data then?

        Could this be a reasonable wait to override the method?

        MyWidget() {
          connect(this, SIGNAL(closingMyWidget(int)), controller, SLOT(onClosingMyWidget(int)));
        }
        ...
        void MyWidget::closeEvent(QCloseEvent*) {
          emit closingMyWidget(_myWidgetId);
        }
        

        And what about all the subwidgets (base classes) created on the heap? Should I destroy them manually or does Qt takes care of them?

        Pl45m4P 1 Reply Last reply
        0
        • D Daniel_Contro

          Hi to everyone,
          I'm developing an application that has several custom widgets dynamically added/removed and I was wondering which is the proper way to close them, keeping in mind that they contain data that should be saved. I read something in the Qt's documentation but I can't comprehend if I should define a slot e.g. onClose() in each widget connected to QWidget::close() or if I should override QWidget::closeEvent().

          B Offline
          B Offline
          Bonnie
          wrote on last edited by
          #4

          @Daniel_Contro said in How to properly close a widget:

          define a slot e.g. onClose() in each widget connected to QWidget::close()

          Just want to say that you cannot connect a slot to another slot / non-signal function...

          1 Reply Last reply
          0
          • D Daniel_Contro

            It's mostly text, but also the widgets order is important to me (it's a hierarchical data structure); I have my own Controller and Model where I should store the data.

            Should I override closeEvent() for every different custom widget that requires saving his data then?

            Could this be a reasonable wait to override the method?

            MyWidget() {
              connect(this, SIGNAL(closingMyWidget(int)), controller, SLOT(onClosingMyWidget(int)));
            }
            ...
            void MyWidget::closeEvent(QCloseEvent*) {
              emit closingMyWidget(_myWidgetId);
            }
            

            And what about all the subwidgets (base classes) created on the heap? Should I destroy them manually or does Qt takes care of them?

            Pl45m4P Offline
            Pl45m4P Offline
            Pl45m4
            wrote on last edited by
            #5

            @Daniel_Contro said in How to properly close a widget:

            Should I destroy them manually or does Qt takes care of them

            If they have a parent, Qt takes care and they get cleaned up together with their parent. If you allocate member vars on heap, you need to take care of them (d'tor).
            https://doc.qt.io/qt-5/objecttrees.html#construction-destruction-order-of-qobjects

            @Daniel_Contro said in How to properly close a widget:

            Could this be a reasonable wait to override the method?

            Yes.


            If debugging is the process of removing software bugs, then programming must be the process of putting them in.

            ~E. W. Dijkstra

            1 Reply Last reply
            0
            • SGaistS Offline
              SGaistS Offline
              SGaist
              Lifetime Qt Champion
              wrote on last edited by
              #6

              Hi,

              How are these widget managed from a data saving point of view ?

              Interested in AI ? www.idiap.ch
              Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

              1 Reply Last reply
              0

              • Login

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