Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PyQT Window: I want to remember the location it was closed at

Tags:

python

pyqt

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?

like image 386
user2315 Avatar asked Jul 05 '12 20:07

user2315


People also ask

How to center a window in PyQt5?

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.

How to create window in PyQt5?

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.

Where is PyQt located?

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 .

How do I close a PyQt window?

The simplest way to close a window is to click the right (Windows) or left (macOS) 'X' button on the title bar.


2 Answers

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.

like image 191
Ronan Paixão Avatar answered Oct 17 '22 00:10

Ronan Paixão


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)
like image 25
HansHarhoff Avatar answered Oct 17 '22 00:10

HansHarhoff