4

I am using PyQt5 5.5.1 (64-bit) with Python 3.4.0 (64-bit) on Windows 8.1 64-bit.

I am having trouble restoring the position and size (geometry) of my very simple PyQt app.

Here is minimal working application:

import sys
from PyQt5.QtWidgets import QApplication, QWidget

class myApp(QWidget):
    def __init__(self):
        super().__init__()
        self.initUI()


    def initUI(self):
        self.show()

if __name__ == '__main__':
    app = QApplication(sys.argv)
    view = myApp()
    sys.exit(app.exec())

What I read online is that this is the default behavior and we need to use QSettings to save and retrieve settings from Windows registry, which is stored in

\\HKEY_CURRENT_USER\Software\{CompanyName}\{AppName}\

Here are some of the links I read.

I could have followed those tutorials but those tutorials/docs were written for C++ users.

C++ is not my glass of beer, and converting those codes are impossible to me.


Related:

QSettings(): How to save to current working directory

1
  • @zetysz Nah! I don't want to directly use it. I want interface with PyQt. What if I later want to port same codebase to Linux? Nov 23, 2015 at 12:54

4 Answers 4

6

This should do.

import sys
from PyQt5.QtWidgets import QApplication, QWidget
from PyQt5.QtCore import QSettings, QPoint, QSize

class myApp(QWidget):
    def __init__(self):
        super(myApp, self).__init__()

        self.settings = QSettings( 'My company', 'myApp')     

        # Initial window size/pos last saved. Use default values for first time
        self.resize(self.settings.value("size", QSize(270, 225)))
        self.move(self.settings.value("pos", QPoint(50, 50)))

    def closeEvent(self, e):
        # Write window size and position to config file
        self.settings.setValue("size", self.size())
        self.settings.setValue("pos", self.pos())

        e.accept()

if __name__ == '__main__':
    app = QApplication(sys.argv)
    frame = myApp()
    frame.show()
    app.exec_()

I simplified this example: QSettings(): How to save to current working directory

5
  • Oh, sorry, closed my tmp-editor. If you mean the additional parameter in QSettings you may check them in the link I included.
    – Valentin H
    Nov 23, 2015 at 14:14
  • What is that self.tab = QWidget() line doing by the way? Nov 24, 2015 at 15:16
  • @SantoshKumar: Oh, was just an redundant reminder from original code. I fixed
    – Valentin H
    Nov 27, 2015 at 11:01
  • what is e.accept() doing, and where does e come from? o,O
    – Saelyth
    Jul 24, 2017 at 13:09
  • 1
    e.accept() means event.accept() and it comes from the def closeEvent(self, e): line of code Jun 24, 2019 at 15:18
4

Similar to @Valentin's response, because I feel settings are being written to registry, which will be issue for cross compatiblity. Here is the relevant startEvent() and closeEvent() for the job.

def startEvent()
    self.settings = QSettings(QSettings.IniFormat,QSettings.SystemScope, '__MyBiz', '__settings')
    self.settings.setFallbacksEnabled(False)    # File only, not registry or or.

    # setPath() to try to save to current working directory
    self.settings.setPath(QSettings.IniFormat,QSettings.SystemScope, './__settings.ini')

    # Initial window size/pos last saved
    self.resize(self.settings.value("size", QSize(270, 225)))
    self.move(self.settings.value("pos", QPoint(50, 50)))

    self.tab = QWidget()

def closeEvent(self, e):
    # Write window size and position to config file
    self.settings.setValue("size", self.size())
    self.settings.setValue("pos", self.pos())

startEvent() should be initiated at startup and closeEvent() should be taken care before quitting the main window.

1
  • 2
    By default, QSettings stores the settings for each platform in the best appropriate place. On Windows in the registry and on Linux in a config file. You don't have to provide the format type and the location explicitly, when you are comfortable to comply with the usual way the settings are stored on your OS.
    – Valentin H
    Nov 23, 2015 at 14:18
2

You should indeed use QSetting for this.

All the Qt examples have been converted to Python. They are included in the source packages of PyQt (or PySide), which you can download here

You can also look online in the github repo, particularly in application.py of mainwindows example.

def readSettings(self):
    settings = QSettings("Trolltech", "Application Example")
    pos = settings.value("pos", QPoint(200, 200))
    size = settings.value("size", QSize(400, 400))
    self.resize(size)
    self.move(pos)

def writeSettings(self):
    settings = QSettings("Trolltech", "Application Example")
    settings.setValue("pos", self.pos())
    settings.setValue("size", self.size())

Fire writeSettings() before quitting and initiate readSettings() on startup.

1
  • Links are good and it actually solved my problem, but you should consider posting some code for the sake of StackOverflow policies. Nov 23, 2015 at 13:18
1

In my case I use .ini files to store settings (language, default user, ...). the same code works on both Debian and Windows.

An example:

from PySide.QtCore import QSettings

self.settings = QSettings('settings.ini', QSettings.IniFormat)
...
self.settings.setValue('size', self.size())

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.