Brief Introduction
In general, we are in the process of developing software, we will cache some information to the local, you can use the lightweight database SQLite, you can also manipulate the registry, read and write configuration files.
About the use of qsettings has been introduced before, more detailed, see "More Reference", the following describes the following qsettings common functions-Read and write the registry, configuration files. Brief advantages Read-write Registry General storage general read sub-directory storage replacement ApplicationName packet read-write profile General storage general read Packet storage packet read sub-directory store delete content question explanation More reference benefits
You do not need to specify a registry path
In general, we need to define a macro, or a constant string, to specify the location of the saved registry. #define HKEY_CURRENT_USER_QT "HKEY_CURRENT_USER\\SOFTWARE\\DIGIA\\QT" Const QString HKEY_CURRENT_USER_QT = "HKEY_ CURRENT_USER\\SOFTWARE\\DIGIA\\QT ";
No configuration file path specified
In general, we need to define a macro, or a constant string, to specify the location and name of the saved configuration file. #define INI_QT "C:\Users\WangLiang\AppData\Roaming\Digia" Const QString INI_QT = "C:\Users\WangLiang\AppData\Roaming\ Digia ";
In the following way, we do not need to do too much work, QT has been very good for you to achieve. Read and write the registration form General Storage
Here we take QT as an example, it is well known that QT now belongs to Digia, that is to say: The organization named Digia, the product is called QT.
In the main () function, first set the organization name, the product name.
Qcoreapplication::setorganizationname (QString ("Digia"));
Qcoreapplication::setapplicationname (QString ("Qt"));
Then use Qsettings to manipulate the registry:
Qsettings settings (Qsettings::nativeformat, Qsettings::userscope, Qcoreapplication::organizationname (), Qcoreapplication::applicationname ());
Settings.setvalue ("Name", "Qt Creator");
Settings.setvalue ("Version", 5);
At this point, we open the registry regedit and the data is generated.
General Read
After the data is stored, 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
At this point, we can get the output results by looking at the application Output window. Sub-catalog storage
If we need to create multiple subdirectories under the same path, here are two ways to do this. Replace ApplicationName
As above, we can see that the OrganizationName corresponding registry path for the hkey_current_user\\software\\digia,applicationname corresponding to its next level of the directory, Then the sub-catalog needs to change its corresponding applicationname.
Qsettings settings (Qsettings::nativeformat, Qsettings::userscope, QString ("%1\\%2"). Arg (qcoreapplication::o Rganizationname ()). 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");
Grouping
The way to replace ApplicationName looks a bit cumbersome, by contrast, using group grouping is easier.
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 ();
In this case, we look at the registry data again.
Note:
To create a new directory, you need to reopen the registry, and if you add new settings, you do not need to reopen the registry, just switch back and forth the corresponding options.
read-write configuration file General Storage
As above, we only need to change the format from Nativeformat to Iniformat:
Qsettings settings (Qsettings::iniformat, Qsettings::userscope, Qcoreapplication::organizationname (), Qcoreapplication::applicationname ());
Settings.setvalue ("Name", "Qt Creator");
Settings.setvalue ("Version", 5);
At this point, we open the corresponding storage directory, the data is generated.
We can go to the folder: C:\Users\WangLiang\AppData\Roaming (AppData default to hidden files, you need to set the display to view), you can see the generated folder "Digia" and the configuration file "Qt.ini".
General Read
After the data is stored, 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
At this point, we can get the output results by looking at the application Output window. Grouped Storage
We can see that the configuration file contains the default grouping as: General. Usually, we need to classify the configuration, for example: User name, password and other information belong to the user group, product name, version number belongs to the set 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 ();
At this point we look at the configuration file again, which has generated two additional groupings.
Packet Read
settings.begingroup ("Setting");
QString strName = Settings.value ("Name"). ToString ();
int nversion = Settings.value ("Version"). ToInt (); Settings