In this tutorial, I am going to show you how to save your #PyQt5 application settings with #QSettings class.

When you create a desktop application, normally you want to have a “preference” option allowing users to save application settings such as window size, last closed position, font style, background color, etc, so next time when the user re-open the application, all the configurations will resume to the last session.

I will be using Windows 10 for the demonstration. The settings I want to save are window size and last closed position.


Buy Me a Coffee? Your support is much appreciated!
PayPal Me: https://www.paypal.me/jiejenn/5
Venmo: @Jie-Jenn





Source Code:


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

class MyApp(QWidget):
    def __init__(self):
        super().__init__()

        self.settings = QSettings('MyQtApp', 'App1')
        # print(self.settings.fileName())
        try:
            self.reisze(self.settings.value('window size'))
            self.move(self.settings.value('window position'))
        except:
            pass

    def closeEvent(self, event):
        self.settings.setValue('window size', self.size())
        self.settings.setValue('window position', self.pos())

app = QApplication(sys.argv)        

demo = MyApp()
demo.show()

sys.exit(app.exec_())