QSettings Read and Write Registry, Configuration File (C: Users Firecat AppData Roaming liteide liteide.ini)

Keywords: Qt5 Qt

Source of the article: http://blog.csdn.net/liang19890820/article/details/50518187

Read and Write Registry

General storage

Let's take Qt as an example. It is well known that Qt belongs to Digia now. That is to say, the organization name is Digia and the product name is Qt.

In the main() function, the organization name and product name are first set.

QCoreApplication::setOrganizationName(QString("Digia"));
QCoreApplication::setApplicationName(QString("Qt"));
  • 1
  • 2
  • 1
  • 2

Then use QSettings to operate on the registry:

QSettings settings(QSettings::NativeFormat, QSettings::UserScope, QCoreApplication::organizationName(), QCoreApplication::applicationName());

settings.setValue("Name", "Qt Creator");
settings.setValue("Version", 5);
  • 1
  • 2
  • 3
  • 4
  • 1
  • 2
  • 3
  • 4

At this point, we open the registry regedit, and the data is generated.

General reading

After storing the data, the default program needs to load the corresponding data when it starts.

QString strName = settings.value("Name").toString();
int nVersion = settings.value("Version").toInt();
//Name:Qt Creator  Version:5
  • 1
  • 2
  • 3
  • 1
  • 2
  • 3

At this point, we can get the output by looking at the application output window.

Subdirectory Storage

What if we need to create multiple subdirectories in the same path? Here are two ways.

Replace application Name

As we can see above, the registry path corresponding to organization Name is HKEY_CURRENT_USER\\\\\\\ Digia, and the application Name corresponds to its next level directory, so the sub-directory needs to change its corresponding application Name.

QSettings settings(QSettings::NativeFormat, QSettings::UserScope, QString("%1\\%2").arg(QCoreApplication::organizationName()).arg(QCoreApplication::applicationName()), "Qt5.5");

settings.setValue("Name", "Qt Creator");
settings.setValue("Version", "5.5");

QSettings settings2(QString("%1\\%2").arg(QCoreApplication::organizationName()).arg(QCoreApplication::applicationName()), "Qt5.6");

settings2.setValue("Name", "Qt Creator");
settings2.setValue("Version", "5.6");
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9

Grouping

The way to replace application Name seems a bit cumbersome, compared to using group grouping, it's simpler!

QSettings settings;
settings.beginGroup("Qt5.5");
settings.setValue("Name", "Qt Creator");
settings.setValue("Version", "5.5");
settings.endGroup();

settings.beginGroup("Qt5.6");
settings.setValue("Name", "Qt Creator");
settings.setValue("Version", "5.6");
settings.endGroup();
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10

At this point, we look at the registry data again.

Note:
If you create a new directory, you need to reopen the registry. If you add new settings, you don't need to reopen the registry. You just need to switch the corresponding options back and forth.

Read and write configuration files

General storage

As mentioned above, we just need to change the format from Native Format to IniFormat:

QSettings settings(QSettings::IniFormat, QSettings::UserScope, QCoreApplication::organizationName(), QCoreApplication::applicationName());

settings.setValue("Name", "Qt Creator");
settings.setValue("Version", 5);
  • 1
  • 2
  • 3
  • 4
  • 1
  • 2
  • 3
  • 4

At this point, we open the corresponding storage directory, and the data is generated.

We can go to the folder: C: Users WangLiang AppData Roaming (AppData defaults to hidden files, you need to set the display to view), you can see the generated folder "Digia" and configuration file "Qt.ini".

General reading

After storing the data, the default program needs to load the corresponding data when it starts.

QString strName = settings.value("Name").toString();
int nVersion = settings.value("Version").toInt();
//Name:Qt Creator  Version:5
  • 1
  • 2
  • 3
  • 1
  • 2
  • 3

At this point, we can get the output by looking at the application output window.

Packet storage

We can see that the default grouping contained in the configuration file is: General. Usually, we need to classify configuration, such as user name, password and other information belong to user group, product name, version number belong to settings group.

QSettings settings(QSettings::IniFormat, QSettings::UserScope, QCoreApplication::organizationName(), QCoreApplication::applicationName());

settings.beginGroup("Setting");
settings.setValue("Name", "Qt Creator");
settings.setValue("Version", 5);
settings.endGroup();

settings.beginGroup("User");
settings.setValue("UserName", "WangL");
settings.setValue("Password", "123456");
settings.endGroup();
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11

At this point, we look at the configuration file again, which has generated two other groupings.

Packet read

settings.beginGroup("Setting");
QString strName = settings.value("Name").toString();
int nVersion = settings.value("Version").toInt();
settings.endGroup();
//Name:Qt Creator  Version:5

settings.beginGroup("User");
QString strUserName = settings.value("UserName").toString();
QString strPassword = settings.value("Password").toString();
settings.endGroup();
//UserName:WangL  Password:123456
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11

Subdirectory Storage

When do I need subdirectory storage? QQ has been used by everyone. Does every user have a directory corresponding to QQ number, which stores the corresponding information of each user?

As we can see above, the local path corresponding to organization Name is C: Users WangLiang AppData Roaming Digia, and the configuration file corresponding to application Name is below. If we need to create multiple directories under the same path, we need to change the corresponding organization Name, and the name of the configuration file needs to change its corresponding application Name.

QSettings settings(QSettings::IniFormat, QSettings::UserScope, QString("%1\\%2\\%3").arg(QCoreApplication::organizationName()).arg(QCoreApplication::applicationName()).arg("Qt5.5"), "User");
settings.setValue("Name", "Qt Creator");
settings.setValue("Version", "5.5");

QSettings settings2(QSettings::IniFormat, QSettings::UserScope, QString("%1\\%2\\%3").arg(QCoreApplication::organizationName()).arg(QCoreApplication::applicationName()).arg("Qt5.5"), "User");
settings2.setValue("Name", "Qt Creator");
settings2.setValue("Version", "5.6");
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7

At this point, when we look at the local file again, we will find that under the directory where C: Users WangLiang AppData Roaming Digia Qt is located, two folders "Qt5.5 " and "Qt5.6 " will be generated, and the corresponding configuration file User.ini will be generated under each directory.

Delete content

Delete a specified key

QSettings settings;
settings.setValue("Name", "Qt Creator");
settings.setValue("Version", 5);

settings.remove("Name");

QStringList keys = settings.allKeys();
// keys: ["Version"]
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8

Clear all keys

settings.clear(); 
QStringList keys = settings.allKeys();
// keys: []
  • 1
  • 2
  • 3
  • 1
  • 2
  • 3

Delete settings keys and sub-settings keys

QSettings settings;
settings.setValue("Qt5.6", "5.6");

settings.beginGroup("Qt5.5");
settings.setValue("Name", "Qt Creator");
settings.setValue("Version", "5.5");
settings.endGroup();

settings.beginGroup("Qt5.6");
settings.setValue("Name", "Qt Creator");
settings.setValue("Version", "5.6");
settings.endGroup();

settings.remove("Qt5.6");

QStringList strList = settings.allKeys();
// keys: ["Qt5.5/Name", "Qt5.5/Version"]
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17

If the key is an empty string, all keys in the current group() will be deleted.

QSettings settings;
settings.setValue("Qt5.6", "5.6");

settings.beginGroup("Qt5.5");
settings.setValue("Name", "Qt Creator");
settings.setValue("Version", "5.5");
settings.endGroup();

settings.beginGroup("Qt5.6");
settings.setValue("Name", "Qt Creator");
settings.setValue("Version", "5.6");
settings.endGroup();

settings.beginGroup("Qt5.6");
settings.remove("");
settings.endGroup();

QStringList keys = settings.allKeys();
// keys: ["Qt5.5/Name", "Qt5.5/Version"]
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19

Interrogative interpretation

As shown in the code above, we can see that keys before deletion: ["Qt5.6", "Qt5.5/Name", "Qt5.5/Version", "Qt5.6/Name", "Qt5.6/Version"], where Qt5.6 is grouped into the default General.

Then call the following code:

settings.beginGroup("Qt5.6");
settings.remove("");
settings.endGroup();
  • 1
  • 2
  • 3
  • 1
  • 2
  • 3

It should only delete all keys in the corresponding group. That is to say, the remaining keys should be ["Qt5.6", "Qt5.5/Name", "Qt5.5/Version"]. Why will the keys under General be deleted? Okay, let's skip here and go on.

Since the keys under General can be deleted, the corresponding Qt5.6 keys created under Qt5.5 grouping should also be deleted.

Before deletion:

After deletion:

What the devil, why Qt5.6 under Qt5.5 grouping has the corresponding key?

We continue to analyse:

Before deletion: keys: ["Qt5.6", "Qt5.5/Qt5.6", "Qt5.5/Name", "Qt5.5/Version", "Qt5.5/Name", "Qt5.6/Name", "Qt5.6/Version"].

Removes the setkey and any sub-settings of key.

That is to say, even if Qt5.6 exists in Qt5.5/Qt5.6, the set key belongs to Qt5.5 instead of Qt5.6, so it will not be deleted.

In that case, our doubts will disappear.

Posted by blackhorse on Fri, 04 Jan 2019 18:30:11 -0800