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. How do I get a signal to close the main window?

How do I get a signal to close the main window?

Scheduled Pinned Locked Moved Unsolved Qt for Python
8 Posts 2 Posters 8.8k 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.
  • M Offline
    M Offline
    Mikeeeeee
    wrote on 23 Nov 2020, 19:44 last edited by
    #1

    Hi!
    How do I get a signal to close the main window?
    If I open another window but close the main window, I don't get these closing signals:

    self.ui.destroyed.connect(lambda : print(6666666))
    self.destroyed.connect(lambda: print(777777777777))
    

    My class:

    class MainWindow(QMainWindow):
    def __init__(self):
        super(MainWindow, self).__init__()
        self.ui = uic.loadUi("mainWindow.ui")
    

    And not work event :

     def closeEvent(self, event):
            print("closeEvent")
    
    J 1 Reply Last reply 23 Nov 2020, 23:23
    0
    • M Mikeeeeee
      23 Nov 2020, 19:44

      Hi!
      How do I get a signal to close the main window?
      If I open another window but close the main window, I don't get these closing signals:

      self.ui.destroyed.connect(lambda : print(6666666))
      self.destroyed.connect(lambda: print(777777777777))
      

      My class:

      class MainWindow(QMainWindow):
      def __init__(self):
          super(MainWindow, self).__init__()
          self.ui = uic.loadUi("mainWindow.ui")
      

      And not work event :

       def closeEvent(self, event):
              print("closeEvent")
      
      J Offline
      J Offline
      JonB
      wrote on 23 Nov 2020, 23:23 last edited by
      #2

      @Mikeeeeee
      What are you looking for an answer?

      If I open another window but close the main window, I don't get these closing signals:

      Qt has a default behaviour of quitting application when the last window is closed, like if you just have a main window. If you have another window open that won't get fired.

      1 Reply Last reply
      1
      • M Offline
        M Offline
        Mikeeeeee
        wrote on 24 Nov 2020, 07:21 last edited by
        #3

        How do I know that the self.ui window is closed?

        J 1 Reply Last reply 24 Nov 2020, 07:43
        0
        • M Mikeeeeee
          24 Nov 2020, 07:21

          How do I know that the self.ui window is closed?

          J Offline
          J Offline
          JonB
          wrote on 24 Nov 2020, 07:43 last edited by JonB
          #4

          @Mikeeeeee
          If you want to know when a QWidget is closed, you can sub-class and override the closeEvent(). Closing in itself does not emit any signal, so that is the way to recognise it.

          M 1 Reply Last reply 24 Nov 2020, 15:39
          4
          • J JonB
            24 Nov 2020, 07:43

            @Mikeeeeee
            If you want to know when a QWidget is closed, you can sub-class and override the closeEvent(). Closing in itself does not emit any signal, so that is the way to recognise it.

            M Offline
            M Offline
            Mikeeeeee
            wrote on 24 Nov 2020, 15:39 last edited by
            #5

            @JonB But how do I create a subclass for self.ui?

            J 1 Reply Last reply 24 Nov 2020, 16:07
            0
            • M Mikeeeeee
              24 Nov 2020, 15:39

              @JonB But how do I create a subclass for self.ui?

              J Offline
              J Offline
              JonB
              wrote on 24 Nov 2020, 16:07 last edited by
              #6

              @Mikeeeeee
              You don't, it's not a QWidget! self.ui itself does not close or send signals etc. It owns widgets which do.

              M 1 Reply Last reply 24 Nov 2020, 17:53
              2
              • J JonB
                24 Nov 2020, 16:07

                @Mikeeeeee
                You don't, it's not a QWidget! self.ui itself does not close or send signals etc. It owns widgets which do.

                M Offline
                M Offline
                Mikeeeeee
                wrote on 24 Nov 2020, 17:53 last edited by
                #7

                @JonB So how do I know that "mainWindow. ui" is closed?

                J 1 Reply Last reply 24 Nov 2020, 18:39
                0
                • M Mikeeeeee
                  24 Nov 2020, 17:53

                  @JonB So how do I know that "mainWindow. ui" is closed?

                  J Offline
                  J Offline
                  JonB
                  wrote on 24 Nov 2020, 18:39 last edited by JonB
                  #8

                  @Mikeeeeee
                  What class is ui.mainWindow? It needs to be your class derived from QMainWindow. Then you go override that class's closeEvent().

                  I see https://stackoverflow.com/questions/22460003/pyqts-qmainwindow-closeevent-is-never-called is for PyQt4, but look at the solution, I imagine it will be similar for PyQt5/PySide2.

                  1 Reply Last reply
                  0

                  5/8

                  24 Nov 2020, 15:39

                  • Login

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