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. Resetting a PySide6 Window after closing it
Forum Update on Monday, May 27th 2025

Resetting a PySide6 Window after closing it

Scheduled Pinned Locked Moved Solved Qt for Python
qt for pythonpysidepythonpyside2
3 Posts 2 Posters 3.0k 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
    ccortez
    wrote on last edited by ccortez
    #1

    I have been working on a desktop app with PySide6 and have just now caught something that I am not sure how to fix.

    On my GUI I have some windows that open, on the click of a button that's in my 'home' page. The thing that I have noticed is that whenever I open a window, close it, and then open it again, it is in the same state that it was in prior to closing.

    So for example, I have a window that opens that allows the user to make some selections. When I close it and reopen it, all those selections are still there.

    My goal is for the state of that window to reset every time it is closed. I think that in the background, the Qt application is keeping the reference alive and I am not sure how to make sure that the state and everything get destroyed every time I close out a window.

    From what I have googled, I haven't really been able to find a direct solution to this, but I am also fairly new to Qt.

    Any guide or answers would be helpful. Here is a small sample of what my code looks like, if it helps:

    class MainWindow(QMainWindow):
     def __init__(self):
         super(MainWindow, self).__init__()
         # Set settings for main window to pop up on top left of screen
         self.setWindowTitle('Main window')
         #self.setStyleSheet("background-color: #1F4388;")
         self.resize(640, 380)
         left = QScreen.availableGeometry(QApplication.primaryScreen()).topLeft()
         geo = self.frameGeometry()
         geo.moveTopLeft(left)
         self.move(geo.topLeft())
         
        # Button that opens up other window
         self.button = QPushButton(self)       
         self.button.setGeometry(150, 150, 150, 40)       
         self.button.setText('Data Visualization')       
         self.button.setStyleSheet('font-size:15px')       
         self.datavis = DataPlotting()
         self.button.clicked.connect(self.datavis.show)
    
    class DataPlotting1(QMainWindow):
     def __init__(self):
         super(DataPlotting1, self).__init__()
         # Adjusting dimensions of window and the title text size
         self.setWindowTitle('Data Visualization')
         self.resize(640, 900)
         self.label = QLabel(self)
         self.label.setGeometry(120, 0, 400, 100)
         self.label.setText('Data Visualization')
         self.label.setAlignment(Qt.AlignCenter)
         self.label.setStyleSheet('font-size:40px')
    
    if __name__ == '__main__':
     app = QApplication(sys.argv)
     ex = MainWindow()
     ex.show()
     sys.exit(app.exec())
    JonBJ 1 Reply Last reply
    0
    • C ccortez

      I have been working on a desktop app with PySide6 and have just now caught something that I am not sure how to fix.

      On my GUI I have some windows that open, on the click of a button that's in my 'home' page. The thing that I have noticed is that whenever I open a window, close it, and then open it again, it is in the same state that it was in prior to closing.

      So for example, I have a window that opens that allows the user to make some selections. When I close it and reopen it, all those selections are still there.

      My goal is for the state of that window to reset every time it is closed. I think that in the background, the Qt application is keeping the reference alive and I am not sure how to make sure that the state and everything get destroyed every time I close out a window.

      From what I have googled, I haven't really been able to find a direct solution to this, but I am also fairly new to Qt.

      Any guide or answers would be helpful. Here is a small sample of what my code looks like, if it helps:

      class MainWindow(QMainWindow):
       def __init__(self):
           super(MainWindow, self).__init__()
           # Set settings for main window to pop up on top left of screen
           self.setWindowTitle('Main window')
           #self.setStyleSheet("background-color: #1F4388;")
           self.resize(640, 380)
           left = QScreen.availableGeometry(QApplication.primaryScreen()).topLeft()
           geo = self.frameGeometry()
           geo.moveTopLeft(left)
           self.move(geo.topLeft())
           
          # Button that opens up other window
           self.button = QPushButton(self)       
           self.button.setGeometry(150, 150, 150, 40)       
           self.button.setText('Data Visualization')       
           self.button.setStyleSheet('font-size:15px')       
           self.datavis = DataPlotting()
           self.button.clicked.connect(self.datavis.show)
      
      class DataPlotting1(QMainWindow):
       def __init__(self):
           super(DataPlotting1, self).__init__()
           # Adjusting dimensions of window and the title text size
           self.setWindowTitle('Data Visualization')
           self.resize(640, 900)
           self.label = QLabel(self)
           self.label.setGeometry(120, 0, 400, 100)
           self.label.setText('Data Visualization')
           self.label.setAlignment(Qt.AlignCenter)
           self.label.setStyleSheet('font-size:40px')
      
      if __name__ == '__main__':
       app = QApplication(sys.argv)
       ex = MainWindow()
       ex.show()
       sys.exit(app.exec())
      JonBJ Offline
      JonBJ Offline
      JonB
      wrote on last edited by
      #2

      @ccortez
      Normally hiding a window will remember its content if re-opened but closing it will destroy it and its content. You may have to set the Qt.WA_DeleteOnClose attribute on it (https://doc.qt.io/qt-5/qt.html#WidgetAttribute-enum).

      The situation in Python is complicated, because it likes to keep references to objects and retain the object. You show self.datavis = DataPlotting() in your __init__(), that will maintain a reference to that window throughout the lifetime of your MainWindow. Setting that to None, and/or using Qt.WA_DeleteOnClose, are probably required to free the reference properly.

      To fully "reset the state of the window when it is closed" you need to delete it as above and create a new instance when you re-open it. Or, you have to write your own method to reset the size, every widget etc. in it which you call before opening.

      C 1 Reply Last reply
      0
      • JonBJ JonB

        @ccortez
        Normally hiding a window will remember its content if re-opened but closing it will destroy it and its content. You may have to set the Qt.WA_DeleteOnClose attribute on it (https://doc.qt.io/qt-5/qt.html#WidgetAttribute-enum).

        The situation in Python is complicated, because it likes to keep references to objects and retain the object. You show self.datavis = DataPlotting() in your __init__(), that will maintain a reference to that window throughout the lifetime of your MainWindow. Setting that to None, and/or using Qt.WA_DeleteOnClose, are probably required to free the reference properly.

        To fully "reset the state of the window when it is closed" you need to delete it as above and create a new instance when you re-open it. Or, you have to write your own method to reset the size, every widget etc. in it which you call before opening.

        C Offline
        C Offline
        ccortez
        wrote on last edited by
        #3

        @JonB Thank you for this, it helped me solve my issues!

        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