This section contains snippets that were automatically translated from C++ to Python and may contain errors.
Restoring a Window’s Geometry#
How to save & restore window geometry.
This document describes how to save and restore a window’s geometry using the geometry properties. On Windows, this is basically storing the result of
geometry() and calling
setGeometry() in the next session before calling
On X11, this might not work because an invisible window does not have a frame yet. The window manager will decorate the window later. When this happens, the window shifts towards the bottom/right corner of the screen depending on the size of the decoration frame. Although X provides a way to avoid this shift, some window managers fail to implement this feature.
When using Qt Widgets , Qt provides functions that saves and restores a widget window’s geometry and state for you.
saveGeometry() saves the window geometry and maximized/fullscreen state, while
restoreGeometry() restores it. The restore function also checks if the restored geometry is outside the available screen geometry, and modifies it as appropriate if it is:
def closeEvent(self, event): settings = QSettings("MyCompany", "MyApp") settings.setValue("geometry", saveGeometry()) settings.setValue("windowState", saveState()) QMainWindow.closeEvent(event) def readSettings(self): settings = QSettings("MyCompany", "MyApp") restoreGeometry(settings.value("myWidget/geometry").toByteArray()) restoreState(settings.value("myWidget/windowState").toByteArray())
Another solution is to store both
size() and to restore the geometry using
move() before calling
show() , as demonstrated in the Application example.