I have a QDialog, and when the user closes the QDialog, and reopens it later, I want to remember the location and open the window at the exact same spot. How would I exactly remember that location?
The center() method allows the window to be located in the center of the screen. Use the primeGeometry() method to get information about the location and size of the window. Determine the center position of the monitor screen you are using. Move the rectangular position of the window to the center of the screen.
We set the window size using the setGeometry(left,top,width,height) method. The window title is set using setWindowTitle(title). Finally show() is called to display the window.
For anyone stumbling across this post in 2021+ and finding the answers outdated: QT Designer is now in the qt5-applications package, under Qt\bin\ . On Windows this means the default path, for CPython 3.9 using the Python.org installer, is %APPDATA%\Python\Python39\site-packages\qt5_applications\Qt\bin\designer.exe .
The simplest way to close a window is to click the right (Windows) or left (macOS) 'X' button on the title bar.
For that, you can use the saveState()
, saveGeometry()
resize()
and move()
methods, in conjunction with the closeEvent() and QSettings mentioned by the other answers. Here is some example, to get the idea:
class MyWindow(QMainWindow):
def __init__(self, parent):
QMainWindow.__init__(self, parent)
self.settings = QSettings("MyCompany", "MyApp")
self.restoreGeometry(self.settings.value("geometry", ""))
self.restoreState(self.settings.value("windowState", ""))
def closeEvent(self, event):
self.settings.setValue("geometry", self.saveGeometry())
self.settings.setValue("windowState", self.saveState())
QMainWindow.closeEvent(self, event)
EDIT:
Updated answer to use PyQt API v2. If using API v1, you have to manually cast the result of settings.value()
to a ByteArray like
self.restoreState(self.settings.value("windowState").toByteArray())
I also used the window's own size()
and pos()
, since I'm already loading the windows from a .ui
file. You may set it to defaults before those lines if coding the window from scratch. For the state, I'm defaulting to an empty string, which the function happily accepts as an empty ByteArray and does nothing on the first run.
Ronan Paixão's answer is almost correct. When attempting this a got the error:
AttributeError: 'NoneType' object has no attribute 'toByteArray'
this is because there is, at first, no saved geometry and state. Additionally the return value is already a QByteArray. This code works for me:
class MyWindow(QMainWindow):
def __init__(self, parent):
QMainWindow.__init__(self, parent)
self.settings = QSettings("MyCompany", "MyApp")
if not self.settings.value("geometry") == None:
self.restoreGeometry(self.settings.value("geometry"))
if not self.settings.value("windowState") == None:
self.restoreState(self.settings.value("windowState"))
def closeEvent(self, event):
self.settings.setValue("geometry", self.saveGeometry())
self.settings.setValue("windowState", self.saveState())
QMainWindow.closeEvent(self, event)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With