Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. Qt for Python
  4. Connect parent window to child's close event

Connect parent window to child's close event

Scheduled Pinned Locked Moved Unsolved Qt for Python
pyside2python3qmainwindowsignals & slotsparent & child
10 Posts 6 Posters 4.9k 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.
  • P Offline
    P Offline
    pvlopez
    wrote on last edited by pvlopez
    #1

    I have a pyside2 mainwindow that creates a child window:

    self.newnwindow = QMainWindow()
    self.window = Ui_MainWindow()
    self.window.setupUi(self.newwindow)
    self.newwindow.show()
    

    But I really need to perform an action in the parent when the child window is closed.I mean to connect the parent to it's child close event. Something like this pseudocode(preferible in the parent window):

    if(newwindow gets closed):
         **do something**
    

    I can't figure out how to use the QWidget close and closeEvent functions to report to the MainWindow that the NewWindow has been closed.

    jsulmJ 1 Reply Last reply
    0
    • P pvlopez

      I have a pyside2 mainwindow that creates a child window:

      self.newnwindow = QMainWindow()
      self.window = Ui_MainWindow()
      self.window.setupUi(self.newwindow)
      self.newwindow.show()
      

      But I really need to perform an action in the parent when the child window is closed.I mean to connect the parent to it's child close event. Something like this pseudocode(preferible in the parent window):

      if(newwindow gets closed):
           **do something**
      

      I can't figure out how to use the QWidget close and closeEvent functions to report to the MainWindow that the NewWindow has been closed.

      jsulmJ Online
      jsulmJ Online
      jsulm
      Lifetime Qt Champion
      wrote on last edited by
      #2

      @pvlopez Override closeEvent in your second window and from that override emit a signal:

      void MyMainWindow::closeEvent(QCloseEvent *event)
      {
          emit ImClosing();
          QMainWindow::closeEvent(event);
      }
      

      https://forum.qt.io/topic/113070/qt-code-of-conduct

      P 1 Reply Last reply
      2
      • jsulmJ jsulm

        @pvlopez Override closeEvent in your second window and from that override emit a signal:

        void MyMainWindow::closeEvent(QCloseEvent *event)
        {
            emit ImClosing();
            QMainWindow::closeEvent(event);
        }
        
        P Offline
        P Offline
        pvlopez
        wrote on last edited by pvlopez
        #3

        @jsulm thanks for your answer i tried to do that but i can't figure how to make the override work. For example i have this function in the main window and it is never called, maybe I'm dong something wrong or missing something.

        class Ui_MainWindow(QMainWindow):
            def setupUi(self, MainWindow):
              .......
            def retranslateUi(self, MainWindow):
                 ........
            def closeEvent(self,event):
                print("asdfadf")
        

        The same happens in all the child windows i can't seem to make the overrides work.

        JonBJ 2 Replies Last reply
        0
        • P pvlopez

          @jsulm thanks for your answer i tried to do that but i can't figure how to make the override work. For example i have this function in the main window and it is never called, maybe I'm dong something wrong or missing something.

          class Ui_MainWindow(QMainWindow):
              def setupUi(self, MainWindow):
                .......
              def retranslateUi(self, MainWindow):
                   ........
              def closeEvent(self,event):
                  print("asdfadf")
          

          The same happens in all the child windows i can't seem to make the overrides work.

          JonBJ Offline
          JonBJ Offline
          JonB
          wrote on last edited by JonB
          #4

          @pvlopez
          I am confused as to what your "main windows" are, you seem to have multiple ones....

          self.newnwindow = QMainWindow()
          self.newwindow.show()
          

          This is the child window? Then it will need to be sub-classed from QMainWindow, so that you can override its methods like closeEvent().
          If you don't want to sub-class you can probably also achieve it by installing an eventFilter() for the child events in the parent.
          Read https://doc.qt.io/qt-6/eventsandfilters.html for all of this.

          P 1 Reply Last reply
          0
          • JonBJ JonB referenced this topic on
          • J.HilkJ J.Hilk referenced this topic on
          • JonBJ JonB

            @pvlopez
            I am confused as to what your "main windows" are, you seem to have multiple ones....

            self.newnwindow = QMainWindow()
            self.newwindow.show()
            

            This is the child window? Then it will need to be sub-classed from QMainWindow, so that you can override its methods like closeEvent().
            If you don't want to sub-class you can probably also achieve it by installing an eventFilter() for the child events in the parent.
            Read https://doc.qt.io/qt-6/eventsandfilters.html for all of this.

            P Offline
            P Offline
            pvlopez
            wrote on last edited by
            #5

            @JonB when you design the interface with QtDesigner it makes a function called setupUI() to create the widgets and a QMainWindow where the centralWidget is going to be placed. If you don't do this the window never appers in the right way:

                app = QApplication()
                mainwindow = QMainWindow()
                window = Ui_MainWindow()
                window.setupUi(mainwindow)
                mainwindow.show()
                app.exec_()
            

            That's why it seems I have two but one is in top of the other.

            S T 3 Replies Last reply
            0
            • SGaistS SGaist moved this topic from General and Desktop on
            • P pvlopez

              @JonB when you design the interface with QtDesigner it makes a function called setupUI() to create the widgets and a QMainWindow where the centralWidget is going to be placed. If you don't do this the window never appers in the right way:

                  app = QApplication()
                  mainwindow = QMainWindow()
                  window = Ui_MainWindow()
                  window.setupUi(mainwindow)
                  mainwindow.show()
                  app.exec_()
              

              That's why it seems I have two but one is in top of the other.

              S Offline
              S Offline
              StarterKit
              wrote on last edited by StarterKit
              #6

              @pvlopez , here is a small example how it works for me.

              from PySide6.QtCore import Slot, Signal
              from PySide6.QtWidgets import QApplication, QMainWindow, QWidget, QVBoxLayout, QPushButton, QLabel, QDialog
              
              class ChildWnd(QDialog):
                  closed = Signal()
                  def __int__(self):
                      QDialog.__init__(self)
              
                  @Slot()
                  def closeEvent(self, event):
                      self.closed.emit()
                      super().closeEvent(event)
              
              class MainWnd(QMainWindow):
                  def __init__(self):
                      QMainWindow.__init__(self)
                      self.centralwidget = QWidget()
                      self.layout = QVBoxLayout()
                      self.button = QPushButton("Show window")
                      self.button.pressed.connect(self.on_button)
                      self.layout.addWidget(self.button)
                      self.label = QLabel()
                      self.layout.addWidget(self.label)
                      self.centralwidget.setLayout(self.layout)
                      self.setCentralWidget(self.centralwidget)
                      self.child_wnd = None
              
                  @Slot()
                  def on_button(self):
                      self.child_wnd = ChildWnd(self)
                      self.child_wnd.closed.connect(self.on_child_closed)
                      self.label.setText("")
                      self.child_wnd.show()
              
                  @Slot()
                  def on_child_closed(self):
                      self.label.setText("Window closed")
              
              def main():
                  app = QApplication()
                  mainwindow = MainWnd()
                  mainwindow.show()
                  app.exec()
              
              if __name__ == '__main__':
                  main()
              
              1 Reply Last reply
              0
              • P pvlopez

                @JonB when you design the interface with QtDesigner it makes a function called setupUI() to create the widgets and a QMainWindow where the centralWidget is going to be placed. If you don't do this the window never appers in the right way:

                    app = QApplication()
                    mainwindow = QMainWindow()
                    window = Ui_MainWindow()
                    window.setupUi(mainwindow)
                    mainwindow.show()
                    app.exec_()
                

                That's why it seems I have two but one is in top of the other.

                T Offline
                T Offline
                tilz0R
                wrote on last edited by
                #7

                @pvlopez Ui_MainWindow extends QMainWindow? Then youdo not need 2 classes!

                1 Reply Last reply
                0
                • P pvlopez

                  @JonB when you design the interface with QtDesigner it makes a function called setupUI() to create the widgets and a QMainWindow where the centralWidget is going to be placed. If you don't do this the window never appers in the right way:

                      app = QApplication()
                      mainwindow = QMainWindow()
                      window = Ui_MainWindow()
                      window.setupUi(mainwindow)
                      mainwindow.show()
                      app.exec_()
                  

                  That's why it seems I have two but one is in top of the other.

                  T Offline
                  T Offline
                  tilz0R
                  wrote on last edited by
                  #8

                  @pvlopez said in Connect parent window to child's close event:

                  @JonB when you design the interface with QtDesigner it makes a function called setupUI() to create the widgets and a QMainWindow where the centralWidget is going to be placed. If you don't do this the window never appers in the right way:

                      app = QApplication()
                      mainwindow = QMainWindow()
                      window = Ui_MainWindow()
                      window.setupUi(mainwindow)
                      mainwindow.show()
                      app.exec_()
                  

                  That's why it seems I have two but one is in top of the other.

                  You need to rework it a bit

                  class MyMainWindow(QMainWindow):
                      def __init__(self):
                          self.ui = Ui_MainWindow()
                          self.ui.setupUi(self) # Put widgets to this class
                          self.show()
                   
                      # You need closeEvent in a class that is actually shown
                      def closeEvent(self):
                          print('Close event')
                  
                  app = QApplication()
                  window = MyMainWindow()
                  app.exec_()
                  
                  SGaistS 1 Reply Last reply
                  0
                  • P pvlopez

                    @jsulm thanks for your answer i tried to do that but i can't figure how to make the override work. For example i have this function in the main window and it is never called, maybe I'm dong something wrong or missing something.

                    class Ui_MainWindow(QMainWindow):
                        def setupUi(self, MainWindow):
                          .......
                        def retranslateUi(self, MainWindow):
                             ........
                        def closeEvent(self,event):
                            print("asdfadf")
                    

                    The same happens in all the child windows i can't seem to make the overrides work.

                    JonBJ Offline
                    JonBJ Offline
                    JonB
                    wrote on last edited by JonB
                    #9

                    @tilz0R
                    Yes to your skeleton code! :)

                    The OP has:
                    @pvlopez said in Connect parent window to child's close event:

                    class Ui_MainWindow(QMainWindow):

                    They need to change that.

                    1 Reply Last reply
                    0
                    • T tilz0R

                      @pvlopez said in Connect parent window to child's close event:

                      @JonB when you design the interface with QtDesigner it makes a function called setupUI() to create the widgets and a QMainWindow where the centralWidget is going to be placed. If you don't do this the window never appers in the right way:

                          app = QApplication()
                          mainwindow = QMainWindow()
                          window = Ui_MainWindow()
                          window.setupUi(mainwindow)
                          mainwindow.show()
                          app.exec_()
                      

                      That's why it seems I have two but one is in top of the other.

                      You need to rework it a bit

                      class MyMainWindow(QMainWindow):
                          def __init__(self):
                              self.ui = Ui_MainWindow()
                              self.ui.setupUi(self) # Put widgets to this class
                              self.show()
                       
                          # You need closeEvent in a class that is actually shown
                          def closeEvent(self):
                              print('Close event')
                      
                      app = QApplication()
                      window = MyMainWindow()
                      app.exec_()
                      
                      SGaistS Offline
                      SGaistS Offline
                      SGaist
                      Lifetime Qt Champion
                      wrote on last edited by
                      #10

                      @tilz0R in addition to @JonB, don't call self.show in an __init__ method. It's not the role of the widgets to make themselves visible. That is the role of the object or method creating them.

                      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