Save Application Settings (qsettings)
1. Qsettings class
-
Qsettings provides an interface to save the application's current settings, which can easily save the state of the program, such as the window size and position, the selected state of the option, and so on.
-
In Windows systems, the state information of the program is recorded in the registry, and on Mac OS X Systems, the information is recorded in an XML configuration file, and in a Unix system, the INI text file is used. Qsettings is an abstraction of these technologies, making it possible to save and get the application's set state independently of the operating system.
-
The Qsettings API is based on the Qvariant class, when you create a Qsettings object, you must pass the name of the company or organization (QString) and the name of the application (QString) used to construct a Qsettings object.
2. Using Qsettings
(1) Construct a Qsettings object
(2) Add a setting to Settings
/* Wrapmargin is a child key
/* If the same key exists, the value corresponding to the existing key will be replaced by the new value
(3) Remove the settings from the Setttings
3. How qsettings is Organized
(1) Use "/" to denote sub key
-
Qsettings Storage state information is in the form of Key-value, where key is similar to the file path concept, subkey can be defined in the form of a definition file path, such as Finddialog/matchcase, where MatchCase is a subkey;
(2) using BeginGroup () and Endgroup ()
Settings.begingroup ("MainWindow");
Settings.setvalue ("Size", win->size ());
Settings.setvalue ("fullscreen", Win->isfullscreen ());
Settings.endgroup ();
Settings.begingroup ("Outputpanel");
Settings.setvalue ("Visible", panel->isvisible ());
Settings.endgroup ();
Mainwindow/size
Mainwindow/fullscreen
Outputpanel/visible
(3) Get key and sub key
Qsettings settings;
Settings.setvalue ("Fridge/color", qt::white);
Settings.setvalue ("Fridge/size", Qsize (32, 96));
Settings.setvalue ("Sofa", true);
Settings.setvalue ("TV", false);
qstringlist keys = Settings.childkeys ();
Qsettings settings;
Settings.setvalue ("Fridge/color", qt::white);
Settings.setvalue ("Fridge/size", Qsize (32,96));
Settings.setvalue ("Sofa", true);
Settings.setvalue ("TV", false);
Qstringlist groups = Settings.childgroups ();
4. Save and get the program settings
(1) In the main window's constructor, Readsettings ()
void Mainwindow::readsettings ()
{
Qsettings settings ("Software Inc.", "Spreadsheet"); Writes are consistent with the Read settings
Restoregeometry (Settings.value ("geometry"). Tobytearray ());
RecentFiles = Settings.value ("RecentFiles"). Tostringlist ();
Updaterecentfileactions ();
BOOL showgrid = Settings.value ("showgrid", True). Tobool ();
Showgridaction->setchecked (showgrid);
BOOL Autorecalc = Settings.value ("Autorecalc", True). Tobool ();
Autorecalcaction->setchecked (AUTORECALC);
}
(2) When the main window is closed, writesettings ()
void Mainwindow::writesettings ()
{
Qsettings settings ("Software Inc.", "Spreadsheet");
Settings.setvalue ("Geometry", savegeometry ());
Settings.setvalue ("RecentFiles", recentfiles);
Settings.setvalue ("ShowGrid", showgridaction->ischecked ());
Settings.setvalue ("Autorecalc", autorecalcaction->ischecked ());
}
/* This
Save Application Settings (qsettings)