Question

I'm trying to set the path to store some custom settings for my app using QSettings::setPath and defaulFormat but both seem to be ignored when I try and use the settings file.

>>> QSettings.setDefaultFormat(QSettings.IniFormat)
>>> QSettings.setPath(QSettings.IniFormat, QSettings.UserScope, "C:\\")
>>> settings3 = QSettings("Balh", "MyApp")
>>> settings3.fileName()
PyQt4.QtCore.QString(u'\\HKEY_CURRENT_USER\\Software\\Balh\\MyApp')

I would like to stay using settings = QSettings() if I can without having to supply QSettings::IniFormat to it as it is a cross platform app and should use the native format if the custom path isn't set.

Was it helpful?

Solution

The QSettings documentation misleadingly suggests that the code

QSettings settings("Moose Soft", "Facturo-Pro");

is equivalent to

QCoreApplication::setOrganizationName("Moose Soft");
QCoreApplication::setApplicationName("Facturo-Pro");
QSettings settings;

but this is not true. Try this

from PySide import QtCore
QtCore.QSettings.setDefaultFormat(QtCore.QSettings.IniFormat)

settings = QtCore.QSettings("Moose Soft", "Facturo-Pro")
print settings.format()

QtCore.QCoreApplication.setOrganizationName("MooseSoft")
QtCore.QCoreApplication.setApplicationName("Facturo-Pro")
settings = QtCore.QSettings()
print settings.format()

and you will see that only the second constructor uses the default format. And if you look at the QSettings constructor documentation you will see this confirmed:

Example:

QSettings settings("Moose Tech", "Facturo-Pro");

The scope is set to QSettings::UserScope, and the format is set to QSettings.NativeFormat (i.e. calling setDefaultFormat() before calling this constructor has no effect).

Only some of the QSettings constructors honour the default format and you have chosen one that doesn't.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top