QSettings(3qt)
β¨ QSettings(3qt) β¨
okay bestie, welcome to the QSettings(3qt) manual page π this is literally the class that keeps your app's settings persistent and platform-independent, and we're about to read it together like it's the group chat lore drop π
πΈ NAME πΈ
QSettings - Persistent platform-independent application settings β¨
π¦ SYNOPSIS π¦
#include <qsettings.h>
π Public Members
enum Format { Native = 0, Ini }
enum System { Unix = 0, Windows, Mac }
enum Scope { User, Global }
QSettings ()
QSettings ( Format format )
~QSettings ()
bool writeEntry ( const QString & key, bool value )
bool writeEntry ( const QString & key, double value )
bool writeEntry ( const QString & key, int value )
bool writeEntry ( const QString & key, const QString & value )
bool writeEntry ( const QString & key, const QStringList & value )
bool writeEntry ( const QString & key, const QStringList & value, const
QChar & separator ) (obsolete)
QStringList entryList ( const QString & key ) const
QStringList subkeyList ( const QString & key ) const
QStringList readListEntry ( const QString & key, bool * ok = 0 ) const
QStringList readListEntry ( const QString & key, const QChar &
separator, bool * ok = 0 ) const (obsolete)
QString readEntry ( const QString & key, const QString & def =
QString::null, bool * ok = 0 ) const
int readNumEntry ( const QString & key, int def = 0, bool * ok = 0 )
const
double readDoubleEntry ( const QString & key, double def = 0, bool * ok
= 0 ) const
bool readBoolEntry ( const QString & key, bool def = FALSE, bool * ok =
0 ) const
bool removeEntry ( const QString & key )
void insertSearchPath ( System s, const QString & path )
void removeSearchPath ( System s, const QString & path )
void setPath ( const QString & domain, const QString & product, Scope
scope = Global )
void beginGroup ( const QString & group )
void endGroup ()
void resetGroup ()
QString group () const
π DESCRIPTION π
The QSettings class provides persistent platform-independent application settings. π No cap.
On Unix systems, QSettings uses text files to store settings. On Windows systems, QSettings uses the system registry. On Mac OS X, QSettings uses the Carbon preferences API. platform independence, slay β¨
Each setting comprises an identifying key and the data associated with the key. A key is a unicode string which consists of two or more subkeys. A subkey is a slash, '/', followed by one or more unicode characters (excluding slashes, newlines, carriage returns and equals, '=', signs). The associated data, called the entry or value, may be a boolean, an integer, a double, a string or a list of strings. Entry strings may contain any unicode characters. unicode girlies rise π¦
If you want to save and restore the entire desktop's settings, i.e. which applications are running, use QSettings to save the settings for each individual application and QSessionManager to save the desktop's session. iykyk π
Example settings:
/MyCompany/MyApplication/background color
/MyCompany/MyApplication/foreground color
/MyCompany/MyApplication/geometry/x
/MyCompany/MyApplication/geometry/y
/MyCompany/MyApplication/geometry/width
/MyCompany/MyApplication/geometry/height
/MyCompany/MyApplication/recent files/1
/MyCompany/MyApplication/recent files/2
/MyCompany/MyApplication/recent files/3
Each line above is a complete key, made up of subkeys. so many subkeys, so little time π
A typical usage pattern for reading settings at application startup:
QSettings settings;
settings.setPath( "MyCompany.com", "MyApplication" );
QString bgColor = settings.readEntry( "/colors/background", "white" );
int width = settings.readNumEntry( "/geometry/width", 640 );
// ...
A typical usage pattern for saving settings at application exit or 'save preferences':
QSettings settings;
settings.setPath( "MyCompany.com", "MyApplication" );
settings.writeEntry( "/colors/background", bgColor );
settings.writeEntry( "/geometry/width", width );
// ...
A key prefix can be prepended to all keys using beginGroup(). The application of the prefix is stopped using endGroup(). For example:
QSettings settings;
settings.beginGroup( "/MainWindow" );
settings.beginGroup( "/Geometry" );
int x = settings.readEntry( "/x" );
// ...
settings.endGroup();
settings.beginGroup( "/Toolbars" );
// ...
settings.endGroup();
settings.endGroup();
the nesting? it's giving matryoshka doll realness πͺ
You can get a list of entry-holding keys by calling entryList(), and a list of key-holding keys using subkeyList().
QStringList keys = settings.entryList( "/MyApplication" );
// keys contains 'background color' and 'foreground color'.
QStringList keys = settings.entryList( "/MyApplication/recent files" );
// keys contains '1', '2' and '3'.
QStringList subkeys = settings.subkeyList( "/MyApplication" );
// subkeys contains 'geometry' and 'recent files'
QStringList subkeys = settings.subkeyList( "/MyApplication/recent files" );
// subkeys is empty.
Since settings for Windows are stored in the registry there are some size limitations as follows:
- A subkey may not exceed 255 characters.
- An entry's value may not exceed 16,300 characters.
- All the values of a key (for example, all the 'recent files' subkeys values), may not exceed 65,535 characters.
These limitations are not enforced on Unix or Mac OS X. unix said "no limits, just vibes" π§
Warning: Creating multiple, simultaneous instances of QSettings writing to a text file may lead to data loss! This is a known issue which will be fixed in a future release of Qt. bestie, be careful, this is not a drill π
π Notes for Mac OS X Applications π
The location where settings are stored is not formally defined by the CFPreferences API. mysterious, lowkey iconic π΅οΈββοΈ
At the time of writing settings are stored (either on a global or user basis, preferring locally) into a plist file in $ROOT/System/Library/Preferences (in XML format). QSettings will create an appropriate plist file (com.<first group name>.plist) out of the full path to a key. plist girlies, this one's for you π
For further information on CFPreferences see Apple's Specifications.
π§ Notes for Unix Applications π§
There is no universally accepted place for storing application settings under Unix. In the examples the settings file will be searched for in the following directories: <ol type=1> the original said it, not me π
SYSCONF- the default value isINSTALL/etc/settings/opt/MyCompany/share/etc/opt/MyCompany/share/MyApplication/etc$HOME/.qt
When reading settings the files are searched in the order shown above, with later settings overriding earlier settings. Files for which the user doesn't have read permission are ignored. When saving settings QSettings works in the order shown above, writing to the first settings file for which the user has write permission. (INSTALL is the directory where Qt was installed. This can be modified by using the configure script's -prefix argument ) order matters, bestie π
If you want to put the settings in a particular place in the filesystem you could do this:
settings.insertSearchPath( QSettings::Unix, "/opt/MyCompany/share" );
But in practice you may prefer not to use a search path for Unix. For example the following code:
settings.writeEntry( "/MyApplication/geometry/width", width );
will end up writing the "geometry/width" setting to the file $HOME/.qt/myapplicationrc (assuming that the application is being run by an ordinary user, i.e. not by root). not root, bestie, we're just girls πββοΈ
For cross-platform applications you should ensure that the Windows size limitations are not exceeded.
Warning: QSettings doesn't write the settings until it is destroyed so you should construct the QSettings object on the stack. stack it like a stack of pancakes, fr fr π₯
See also Input/Output and Networking and Miscellaneous Classes.
𧬠Member Type Documentation π§¬
QSettings::Format
QSettings::Native- Store the settings in a platform dependent locationQSettings::Ini- Store the settings in a text file
QSettings::Scope
QSettings::Global- Save settings as global as possibleQSettings::User- Save settings in user space
QSettings::System
QSettings::Mac- Macintosh execution environmentsQSettings::Unix- Mac OS X, Unix, Linux and Unix-like execution environmentsQSettings::Windows- Windows execution environments
π MEMBER FUNCTION DOCUMENTATION π
QSettings::QSettings ()
Creates a settings object. a new QSettings just dropped β¨
Be aware that you must call setPath() or insertSearchPath() before you can use the QSettings object. no skipping this step, bestie π
QSettings::QSettings ( Format format )
Creates a settings object. If format is 'Ini' the settings will be stored in a text file, using the Unix strategy (see above). If format is 'Native', the settings will be stored in a platform specific way (ie. the Windows registry). choose your fighter π
Be aware that you must call setPath() or insertSearchPath() before you can use the QSettings object.
QSettings::~QSettings ()
Destroys the settings object. All modifications made to the settings will automatically be saved. the glow-down that saves your data πΎ
void QSettings::beginGroup ( const QString & group )
Appends group to the current key prefix.
QSettings settings;
settings.beginGroup( "/MainWindow" );
// read values
settings.endGroup();
void QSettings::endGroup ()
Undo previous calls to beginGroup(). Note that a single beginGroup("a/b/c") is undone by a single call to endGroup(). one undo, no cap π
QSettings settings;
settings.beginGroup( "/MainWindow/Geometry" );
// read values
settings.endGroup();
QStringList QSettings::entryList ( const QString & key ) const
Returns a list of the keys which contain entries under key. Does not return any keys that contain subkeys. entries only, no subkeys allowed β¨
Example settings:
/MyCompany/MyApplication/background color
/MyCompany/MyApplication/foreground color
/MyCompany/MyApplication/geometry/x
/MyCompany/MyApplication/geometry/y
/MyCompany/MyApplication/geometry/width
/MyCompany/MyApplication/geometry/height
QStringList keys = settings.entryList( "/MyCompany/MyApplication" );
In the above example, keys will contain 'background color' and 'foreground color'. It will not contain 'geometry' because this key contains subkeys not entries. geometry said "not today" π
To access the geometry values, you could either use subkeyList() to read the keys then read each entry, or simply read each entry directly by specifying its full key, e.g." /MyCompany/MyApplication/geometry/y". iykyk π
See also subkeyList().
QString QSettings::group () const
Returns the current key prefix, or a null string if there is no key prefix set. null string energy π
See also beginGroup().
void QSettings::insertSearchPath ( System s, const QString & path )
ok so this one inserts path into the settings search path, bestie π
and the semantics of path depend on which system s you're talking about. ngl it's usually easier and better to just use setPath() instead of this function, okay? fr fr.
when s is Windows and the execution environment is not Windows, this function does nothing. same energy when s is Unix and the execution environment is not Unix β it does nothing. π«
when s is Windows, and the execution environment IS Windows, the search path list gets used as the first subfolder of the "Software" folder in the registry.
so when you're reading settings, the folders are searched forwards from the first folder (listed below) to the last, returning the first settings found, and ignoring any folders where the user doesn't have read permission. the order is:
HKEY_CURRENT_USER/Software/MyCompany/MyApplicationHKEY_LOCAL_MACHINE/Software/MyCompany/MyApplicationHKEY_CURRENT_USER/Software/MyApplicationHKEY_LOCAL_MACHINE/Software/MyApplication
like, check this out:
QSettings settings;
settings.insertSearchPath( QSettings::Windows, "/MyCompany" );
settings.writeEntry( "/MyApplication/Tip of the day", TRUE );
the code above will write the subkey "Tip of the day" into the first of the registry folders listed below that is found and for which the user has write permission β¨:
HKEY_LOCAL_MACHINE/Software/MyCompany/MyApplicationHKEY_CURRENT_USER/Software/MyCompany/MyApplicationHKEY_LOCAL_MACHINE/Software/MyApplicationHKEY_CURRENT_USER/Software/MyApplication
if a setting is found in the HKEY_CURRENT_USER space, this setting is overwritten independently of write permissions in the HKEY_LOCAL_MACHINE space. π
now when s is Unix, and the execution environment is Unix, the search path list gets used when trying to determine a suitable filename for reading and writing settings files. by default, there are two entries in the search path:
SYSCONFβ whereSYSCONFis a directory specified when configuring Qt; by default it isINSTALL/etc/settings.$HOME/.qt/β where$HOMEis the user's home directory.
all insertions into the search path will go before $HOME/.qt/. for example:
QSettings settings;
settings.insertSearchPath( QSettings::Unix, "/opt/MyCompany/share/etc" );
settings.insertSearchPath( QSettings::Unix, "/opt/MyCompany/share/MyApplication/etc" );
// ...
will result in a search path of:
SYSCONF/opt/MyCompany/share/etc/opt/MyCompany/share/MyApplication/etc$HOME/.qt
when reading settings the files are searched in the order shown above, with later settings overriding earlier settings. files for which the user doesn't have read permission are ignored. when saving settings QSettings works in the order shown above, writing to the first settings file for which the user has write permission. π
note that paths in the file system are not created by this function, so they must already exist to be useful. like, it won't manifest them for you bestie π§
settings under Unix are stored in files whose names are based on the first subkey of the key (not including the search path). the algorithm for creating names is essentially: lowercase the first subkey, replace spaces with underscores and add 'rc', e.g. /MyCompany/MyApplication/background color will be stored in myapplicationrc (assuming that /MyCompany is part of the search path).
see also removeSearchPath().
example: chart/chartform.cpp.
bool QSettings::readBoolEntry ( const QString & key, bool def = FALSE, bool * ok = 0 ) const
reads the entry specified by key, and returns a bool, or the default value, def, if the entry couldn't be read. if ok is non-null, *ok is set to TRUE if the key was read, FALSE otherwise. π
see also readEntry(), readNumEntry(), readDoubleEntry(), writeEntry(), and removeEntry().
double QSettings::readDoubleEntry ( const QString & key, double def = 0, bool * ok = 0 ) const
reads the entry specified by key, and returns a double, or the default value, def, if the entry couldn't be read. if ok is non-null, *ok is set to TRUE if the key was read, FALSE otherwise. π
see also readEntry(), readNumEntry(), readBoolEntry(), writeEntry(), and removeEntry().
QString QSettings::readEntry ( const QString & key, const QString & def = QString::null, bool * ok = 0 ) const
reads the entry specified by key, and returns a QString, or the default value, def, if the entry couldn't be read. if ok is non-null, *ok is set to TRUE if the key was read, FALSE otherwise. β¨
see also readListEntry(), readNumEntry(), readDoubleEntry(), readBoolEntry(), writeEntry(), and removeEntry().
QStringList QSettings::readListEntry ( const QString & key, bool * ok = 0 ) const
reads the entry specified by key as a string. if ok is not 0, *ok is set to TRUE if the key was read, otherwise *ok is set to FALSE.
note that if you want to iterate over the list, you should iterate over a copy, e.g.
QStringList list = mySettings.readListEntry( "recentfiles" );
QStringList::Iterator it = list.begin();
while( it != list.end() ) {
myProcessing( *it );
++it;
}
see also readEntry(), readDoubleEntry(), readBoolEntry(), writeEntry(), removeEntry(), and QStringList::split().
QStringList QSettings::readListEntry ( const QString & key, const QChar & separator, bool * ok = 0 ) const
this is an overloaded member function, provided for convenience. it behaves essentially like the above function. π¦
this function is obsolete. it is provided to keep old source working. we strongly advise against using it in new code. π
reads the entry specified by key as a string. the separator is used to create a QStringList by calling QStringList::split(separator, entry). if ok is not 0: *ok is set to TRUE if the key was read, otherwise *ok is set to FALSE.
warning: as the documentation states,QStringList::split()will omit empty strings from the list. because of this, it is impossible to retrieve identical list data with this function. we recommend using thereadListEntry()andwriteEntry()overloads that do not take aseparatorargument. π
note that if you want to iterate over the list, you should iterate over a copy, e.g.
QStringList list = mySettings.readListEntry( "size", " " );
QStringList::Iterator it = list.begin();
while( it != list.end() ) {
myProcessing( *it );
++it;
}
see also readEntry(), readDoubleEntry(), readBoolEntry(), writeEntry(), removeEntry(), and QStringList::split().
int QSettings::readNumEntry ( const QString & key, int def = 0, bool * ok = 0 ) const
reads the entry specified by key, and returns an integer, or the default value, def, if the entry couldn't be read. if ok is non-null, *ok is set to TRUE if the key was read, FALSE otherwise. πΈ
see also readEntry(), readDoubleEntry(), readBoolEntry(), writeEntry(), and removeEntry().
bool QSettings::removeEntry ( const QString & key )
removes the entry specified by key. π§Ή
returns true if the entry was successfully removed; otherwise returns false. note that removing the last entry in any given folder, will also remove the folder. (she cleans up after herself, we love that π
)
see also readEntry() and writeEntry().
void QSettings::removeSearchPath ( System s, const QString & path )
removes all occurrences of path (using exact matching) from the settings search path for system s. note that the default search paths cannot be removed. no take-backs on those bestie π«Ά
see also insertSearchPath().
void QSettings::resetGroup ()
set the current key prefix to the empty string. β¨ clean slate energy.
void QSettings::setPath ( const QString & domain, const QString & product, Scope scope = Global )
insert platform-dependent paths from platform-independent information. iconic β¨
the domain should be an Internet domain name controlled by the producer of the software, eg. Trolltech products use "trolltech.com".
the product should be the official name of the product.
the scope should be QSettings::User for user-specific settings, or QSettings::Global for system-wide settings (generally these will be read-only to many users).
not all information is relevant on all systems. π€·
QStringList QSettings::subkeyList ( const QString & key ) const
returns a list of the keys which contain subkeys under key. does not return any keys that contain entries. π«
example settings:
/MyCompany/MyApplication/background color
/MyCompany/MyApplication/foreground color
/MyCompany/MyApplication/geometry/x
/MyCompany/MyApplication/geometry/y
/MyCompany/MyApplication/geometry/width
/MyCompany/MyApplication/geometry/height
/MyCompany/MyApplication/recent files/1
/MyCompany/MyApplication/recent files/2
/MyCompany/MyApplication/recent files/3
QStringList keys = settings.subkeyList( "/MyCompany/MyApplication" );
in the above example, keys will contain 'geometry' and 'recent files'. it will not contain 'background color' or 'foreground color' because those keys contain entries not subkeys. to get a list of keys that contain entries rather than subkeys use entryList() instead. π
warning: in the above example, if QSettings is writing to an Ini file, then a call tosubkeyList("/MyCompany")will return an empty list. this happens because a key like/MyCompany/MyApplication/background coloris written to the file"mycompanyrc", under the section[MyApplication]. this call is therefore a request to list the sections in an ini file, which is not supported in this version of QSettings. this is a known issue which will be fixed in Qt-4. π
see also entryList().
bool QSettings::writeEntry ( const QString & key, bool value )
writes the boolean entry value into key key. the key is created if it doesn't exist. any previous value is overwritten by value. π
if an error occurs the settings are left unchanged and FALSE is returned; otherwise TRUE is returned.
warning: on certain platforms, keys are required to contain at least two components (e.g., "/foo/bar"). this limitation does not apply to Qt 4. π
see also readListEntry(), readNumEntry(), readDoubleEntry(), readBoolEntry(), and removeEntry().
example: chart/chartform.cpp.
bool QSettings::writeEntry ( const QString & key, double value )
this is an overloaded member function, provided for convenience. it behaves essentially like the above function. π¦
writes the double entry value into key key. the key is created if it doesn't exist. any previous value is overwritten by value.
if an error occurs the settings are left unchanged and FALSE is returned; otherwise TRUE is returned.
see also readListEntry(), readNumEntry(), readDoubleEntry(), readBoolEntry(), and removeEntry().
bool QSettings::writeEntry ( const QString & key, int value )
okay so this one is an overloaded member function, provided for convenience β basically it does the same thing as the one above it, just for a different type. it's giving "same vibe, different flavor" β¨
It writes the integer entry value into the key key. The key gets created if it doesn't exist yet (yes bestie, it manifests itself π
), and any previous value gets overwritten by value. no sentimentality, no keeping the ex.
If an error occurs, the settings are left unchanged and FALSE is returned; otherwise TRUE is returned.
See also readListEntry(), readNumEntry(), readDoubleEntry(), readBoolEntry(), and removeEntry().
bool QSettings::writeEntry ( const QString & key, const QString & value )
another overloaded member function, provided for convenience, behaving essentially like the one above. the whole family is so consistent honestly, we stan πΈ
It writes the string entry value into the key key. The key is created if it doesn't exist. Any previous value is overwritten by value. And here's the lil detail: if value is an empty string or a null string, the key's value will be an empty string. so it doesn't ghost you, it just shows up empty π
If an error occurs the settings are left unchanged and FALSE is returned; otherwise TRUE is returned.
See also readListEntry(), readNumEntry(), readDoubleEntry(), readBoolEntry(), and removeEntry().
bool QSettings::writeEntry ( const QString & key, const QStringList & value )
yep, another overloaded member function, provided for convenience, behaving essentially like the above function. the overloads are MULTIPLYING and honestly? iconic π
It writes the string list entry value into the key key. The key is created if it doesn't exist. Any previous value is overwritten by value.
If an error occurs the settings are left unchanged and FALSE is returned; otherwise returns TRUE.
See also readListEntry(), readNumEntry(), readDoubleEntry(), readBoolEntry(), and removeEntry().
bool QSettings::writeEntry ( const QString & key, const QStringList & value, const QChar & separator )
Overloaded member function, provided for convenience, behaving essentially like the above function.
π¨ This function is obsolete. It is provided to keep old source working. We strongly advise against using it in new code. like, it's giving vintage but not in the cute thrifted way, it's giving "why is this still in my closet" π
It writes the string list entry value into key key. The key is created if it doesn't exist. Any previous value is overwritten by value. The list is stored as a sequence of strings separated by separator (using QStringList::join()), so none of the strings in the list should contain the separator. If the list is empty or null, the key's value will be an empty string.
β οΈ Warning: The list should not contain empty or null strings, becausereadListEntry()will useQStringList::split()to recreate the list. As the documentation states,QStringList::split()will omit empty strings from the list. Because of this, it is impossible to retrieve identical list data that is stored with this function. We recommend using thewriteEntry()andreadListEntry()overloads that do not take aseparatorargument. fr fr, save yourself the heartbreak ππ«Ά
If an error occurs the settings are left unchanged and FALSE is returned; otherwise returns TRUE.
See also readListEntry(), readNumEntry(), readDoubleEntry(), readBoolEntry(), removeEntry(), and QStringList::join().
β¨ SEE ALSO β¨
π COPYRIGHT π
Copyright 1992-2007 Trolltech ASA, http://www.trolltech.com. See the license file included in the distribution for a complete license statement.
π AUTHOR π
Generated automatically from the source code.
(not me being generated automatically, the robots really did that π )
π¦ BUGS π¦
If you find a bug in Qt, please report it as described in http://doc.trolltech.com/bughowto.html. Good bug reports help us to help you. Thank you. π«Ά
The definitive Qt documentation is provided in HTML format; it is located at $QTDIR/doc/html and can be read using Qt Assistant or with a web browser. This man page is provided as a convenience for those users who prefer man pages, although this format is not officially supported by Trolltech. (a lil treat for the man-page girlies, no cap π)
If you find errors in this manual page, please report them to qt-bugs@trolltech.com. Please include the name of the manual page (qsettings.3qt) and the Qt version (3.3.8).
and that's a wrap bestie!! π this whole thing came from Trolltech AS, dated 2 February 2007, for the QSettings(3qt) page β go forth and write your entries, you absolute main character ππ
π¦ where this page comes from
- fedora 43: qt3-devel-docs 3.3.8b-102.fc43 Β· QPL-1.0 OR GPL-2.0-only OR GPL-3.0-only
- fedora 44: qt3-devel-docs 3.3.8b-103.fc44 Β· QPL-1.0 OR GPL-2.0-only OR GPL-3.0-only
- Copyright 1992-2007 Trolltech ASA. All rights reserved. See the
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms