Resetting a PySide6 Window after closing it
-
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())
-
@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 theQt.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 yourMainWindow
. Setting that toNone
, and/or usingQt.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.