Settings

Overview

qtypy.settings.Settings:
Pythonic wrapper for QSettings

Example

from qtypy.settings import Settings

settings = Settings()
with settings.grouped('Geometry'):
    mainWindowGeometry = settings.value('MainWindow') if 'MainWindow' in settings else None

for item in settings.array('RecentFiles'):
    self.addRecentFile(item.value('Path'))

...

with Settings() as settings: # So that sync() is called on exit
    settings.setValue('MainWindow', mainWindowGeometry)
    with settings.array('newarray') as items:
        for path in self.recentFiles():
            items.add(Path=path)

Reference

Pythonic wrapper for QSettings

class qtypy.settings.Settings[source]

Settings class. You can also use this as a context manager so that sync() is called on exit.

items()[source]

Yields all key/value pairs (recursively). Affected by the current group.

keys()[source]

Yield all child keys. Affected by the current group.

keyValues()[source]

Yield all child key/value pairs. Affected by the current group.

groups()[source]

Yield all child groups. Affected by the current group.

grouped(name)[source]

Context manager to enter a settings group. So instead of doing

settings.beginGroup('spam')
try:
    ...
finally:
    settings.endGroup()

you can do

with settings.grouped('spam'):
    ...
array(name)[source]

Array support. This is used both to read a settings array (through iteration) and to write to it (using with). Example of reading:

for item in settings.array('myarray'):
    spam = item.value('spam')
    eggs = item.value('eggs')

is equivalent to:

for index in range(settings.beginReadArray('myarray')):
    spam = settings.value('spam')
    eggs = settings.value('eggs')
settings.endArray()

Example of writing:

with settings.array('myarray') as items:
    items.add(spam='spam', eggs='eggs')
    items.add(spam=42, eggs=13)

is equivalent to:

settings.beginWriteArray('myarray')
settings.setArrayIndex(0)
settings.setValue('spam', 'spam')
settings.setValue('eggs', 'eggs')
settings.setArrayIndex(1)
settings.setValue('spam', 42)
settings.setValue('eggs', 13)
settings.endArray()