Basic Operations for Qt Configuration Files
Using QSettings makes it easy to read and write .ini config files. This note summarizes the most common operations: initialization, reading, writing, deleting, and syncing, useful for quick reference.
Initialization
If you want to work directly with a .ini file, initialize like this:
1 | |
Where:
configPathis the config file pathQSettings::IniFormatmeans using the ini format
Config file example
Assume the config file contains:
1 | |
Reading config
Direct read by path
This is the simplest way to read a single value:
1 | |
It is recommended to pass a default value to value(). Then even if the key does not exist, the program still gets a usable result.
Read within a group
If you need to read many items in the same group, using groups is clearer:
1 | |
Writing config
setValue() automatically creates a config item if it does not exist.
1 | |
Deleting config
Delete a single item:
1 | |
Delete an entire group:
1 | |
Sync to disk
In most cases, QSettings saves automatically, so you do not need to call a save function.
If you want to write immediately to disk, call:
1 | |
For example, sync once before program exit or after changing important settings.
Summary
Common QSettings operations are straightforward. Remember these basics:
value()reads configsetValue()writes configremove()deletes configbeginGroup()/endGroup()handles groupssync()writes immediately to disk
For typical desktop app settings, QSettings is usually enough.