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
2
3
4
5
#include <QCoreApplication>
#include <QSettings>

QString configPath = QCoreApplication::applicationDirPath() + "/config.ini";
QSettings ini(configPath, QSettings::IniFormat);

Where:

  • configPath is the config file path
  • QSettings::IniFormat means using the ini format

Config file example

Assume the config file contains:

1
2
3
4
5
6
7
[network]
host=127.0.0.1
port=8080

[user]
name=admin
remember=true

Reading config

Direct read by path

This is the simplest way to read a single value:

1
2
3
QString host = ini.value("network/host").toString();
int port = ini.value("network/port", 80).toInt();
bool remember = ini.value("user/remember", false).toBool();

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
2
3
4
ini.beginGroup("user");
QString name = ini.value("name").toString();
bool remember = ini.value("remember", false).toBool();
ini.endGroup();

Writing config

setValue() automatically creates a config item if it does not exist.

1
2
3
4
5
6
7
ini.setValue("network/host", "192.168.1.10");
ini.setValue("network/port", 8080);

ini.beginGroup("user");
ini.setValue("name", "admin");
ini.setValue("remember", true);
ini.endGroup();

Deleting config

Delete a single item:

1
ini.remove("user/name");

Delete an entire group:

1
ini.remove("user");

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
ini.sync();

For example, sync once before program exit or after changing important settings.

Summary

Common QSettings operations are straightforward. Remember these basics:

  • value() reads config
  • setValue() writes config
  • remove() deletes config
  • beginGroup() / endGroup() handles groups
  • sync() writes immediately to disk

For typical desktop app settings, QSettings is usually enough.


Basic Operations for Qt Configuration Files
https://greatzaochen.dev/en/posts/8c57ed3c/
Author
Zao_chen
Posted on
January 31, 2026
Licensed under