Pyqt5 QSettings. how to use QSettings

Heads up! You've already completed this tutorial.

meshelleva4815 | 2021-05-07 20:58:01 UTC | #1

Hi Martin's, please can you help on a tutorial on how to use QSettings. am currently having issues in using the module or library 😀😀 in my current project, I have so many options that I need the settings to enable or disable, I tried the documentation but it's really not helping. I just need an in-depth or even if it's a demo on how to use the settings. I count much on your respond soon boss. how is your baby..? good morning

mike2750 | 2021-05-07 20:58:10 UTC | #2

yeah this is a very confusing thing once i learned paid off nicely. I'm dumping some examples of stuff to give you an idea of how i used it with related helpful links.

Here's a test script to set and view settings.

import sys
import os
from PyQt5 import QtCore

settings = QtCore.QSettings('WizardAssistant', 'WizardAssistantDesktop')
settings.setValue('wizardwebsshport', 8889)
#settings.setValue('theme_selection', 'Dark')
#settings.setValue('license', 'XXXXX-XXXX-XXXXX-XXXX')
keys = settings.allKeys()


Here's how you can define and load settings from Qsettings in app

settings = QtCore.QSettings('WizardAssistant', 'WizardAssistantDesktop')
config_data_dir = Path("WizardAssistant/WizardAssistantDesktop")

license_file = QStandardPaths.writableLocation(
    QStandardPaths.AppConfigLocation) / config_data_dir / 'wizardassistant_license.skm'

# Restore settings if exist or set a default
if settings.contains("theme_selection"):
    # there is the key in QSettings
    print('Checking for theme preference in config')
    theme_selection = settings.value('theme_selection')
    print('Found theme_selection in config:' + theme_selection)
    print('theme_selection not found in config. Using default Darkmode')
    settings.setValue('theme_selection', 'Dark')

if settings.contains("panel_name"):
    # there is the key in QSettings
    print('Checking for default panel_name in config')
    panel_name = settings.value('panel_name')
    print('Found panel_name in config:' + str(panel_name))
    print('panel_name not found in config. Using default: Cyberpanel')
    settings.setValue('panel_name', 'cyberpanel')
    panel_name = str(settings.value('panel_name'))

class AppContext(ApplicationContext):
    def run(self):
        global version, current_version, current_release_notes_url, license_type, trial_license, lite_license, premium_license, professional_license, corporate_license, license_expiration, license_key_is_valid, license_key, license_string, current_release_url
        version = self.build_settings['version']
        # Splash startup screen initialization
        self.splash = QSplashScreen(
            QPixmap(os.path.join(os.path.abspath(os.path.dirname(sys.argv[0])), 'images', 'chevron_logo.png')))
        self.settings = QSettings()

        def load_license_from_config():
            global license_string
            if self.settings.contains("license"):
                # there is the key in QSettings
                print('Checking for license in config')
                license_string = self.settings.value('license')
                print('Found license in config:' + license_string)
                print('License not found in config')

Resource links I found helpful when learning and saved.

On that same path you will probably want to know where these Qsettings are stored and control that possibly so ill leave this related information here to hopefully save you and others time.

Qstandardpaths test file shows you paths of where things would go on that OS when run.

from PyQt5 import QtCore
from PyQt5.QtCore import QStandardPaths


std_paths_list_names = ['QStandardPaths.DesktopLocation', 'QStandardPaths.DocumentsLocation',
                        'QStandardPaths.FontsLocation', 'QStandardPaths.ApplicationsLocation',
                        'QStandardPaths.MusicLocation', 'QStandardPaths.MoviesLocation',
                        'QStandardPaths.PicturesLocation', 'QStandardPaths.TempLocation', 'QStandardPaths.HomeLocation',
                        'QStandardPaths.DataLocation', 'QStandardPaths.CacheLocation',
                        'QStandardPaths.GenericCacheLocation', 'QStandardPaths.GenericDataLocation',
                        'QStandardPaths.RuntimeLocation', 'QStandardPaths.ConfigLocation',
                        'QStandardPaths.DownloadLocation', 'QStandardPaths.GenericConfigLocation',
                        'QStandardPaths.AppDataLocation', 'QStandardPaths.AppLocalDataLocation',
std_paths_list = [QStandardPaths.DesktopLocation, QStandardPaths.DocumentsLocation, QStandardPaths.FontsLocation,
                  QStandardPaths.ApplicationsLocation, QStandardPaths.MusicLocation, QStandardPaths.MoviesLocation,
                  QStandardPaths.PicturesLocation, QStandardPaths.TempLocation, QStandardPaths.HomeLocation,
                  QStandardPaths.DataLocation, QStandardPaths.CacheLocation, QStandardPaths.GenericCacheLocation,
                  QStandardPaths.GenericDataLocation, QStandardPaths.RuntimeLocation, QStandardPaths.ConfigLocation,
                  QStandardPaths.DownloadLocation, QStandardPaths.GenericConfigLocation, QStandardPaths.AppDataLocation,
                  QStandardPaths.AppLocalDataLocation, QStandardPaths.AppConfigLocation]

for path in std_paths_list:


Related resources:

meshelleva4815 | 2020-10-10 07:23:53 UTC | #3

😱😱I can't thank you enough Mike. this far more than I was expecting. I really appreciate. good days ahead I hope and pray...thank you

mike2750 | 2020-10-10 09:20:46 UTC | #4

My pleasure. Always happy to help. some of this stuff is a real pita due to limited examples of real usage so involves a lot of trial and error.

I love to share and save others from the same frustration I experienced. :)

I definitely think both of these topics would make for nice tutorial ideas.

The Qsettings thing had i learned about it sooner would have saved me a ton of time. I was having trouble sharing what port a service was listening on between two different processes in the app one being a service/daemon and had i know i could have used Qsettings to share the current port atomicly without dealing with temporary files reading/writing that would have saved me a week of my time off being frustrated with why i was not able to read/write to installation path from frozen/installed app lol.

There is so many cool ways to use that and my examples are pretty basic. I don't know fully what all settings I'm going to have but i will probably build them into a dictionary or list and loop through loading/restoring them vs the one by one approach ive been doing as i add new ones.

Over 10,000 developers have bought Create GUI Applications with Python & Qt!
Create GUI Applications with Python & Qt5
Take a look

Downloadable ebook (PDF, ePub) & Complete Source code

Also available from Leanpub and Amazon Paperback

[[ discount.discount_pc ]]% OFF for the next [[ discount.duration ]] [[discount.description ]] with the code [[ discount.coupon_code ]]

Purchasing Power Parity

Developers in [[ country ]] get [[ discount.discount_pc ]]% OFF on all books & courses with code [[ discount.coupon_code ]]
Well done, you've finished this tutorial! Mark As Complete
[[ user.completed.length ]] completed [[ user.streak+1 ]] day streak

Pyqt5 QSettings. how to use QSettings was written by Martin Fitzpatrick .

Martin Fitzpatrick has been developing Python/Qt apps for 8 years. Building desktop applications to make data-analysis tools more user-friendly, Python was the obvious choice. Starting with Tk, later moving to wxWidgets and finally adopting PyQt.