Save Application Settings (qsettings)

Source: Internet
Author: User

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

    • Qsettings settings ("Mysoft", "Star Runner");

(2) Add a setting to Settings

    • The settings of the program are saved in the Qsettings object in the form of "Key-value". Where key is defined by a QString type, value is defined by the Qvariant type:

    • Settings.setvalue ("Editor/wrapmargin", 68);

/* 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

    • You can also remove the value from the settings by key:

    • int margin = Settings.value ("Editor/wrapmargin"). ToInt ();

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 ()

    • void Qsettings:: BeginGroup (const QString &prefix) is the function of adding prefix after the current group. The current group is automatically added to the tail of a Qsettings object:

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 ();

    • When this is set, the current settings object should look like this hierarchy:

Mainwindow/size

Mainwindow/fullscreen

Outputpanel/visible

(3) Get key and sub key

    • Qstringlist qsettings:: Childkeys () The Const function returns all top-level keys, forming a qstringlist as a return value. For example:

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 ();

    • So it should look like this in the keys:

    • Keys: ["Sofa", "TV"]

    • Qstringlist qsettings:: childgroups () const returns all top-level groups that contain a key, forming a qstringlist as the return value:

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 ();

    • Then groups looks like this:

    • Groups: ["fridge"]

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)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.