when the application has set preferences, it is necessary to save the user's settings (always can not let the user open the software every time to set it up again ...) )。 To save user settings, QT provides the Qsetting class, can be easily completed, but also can be accessed across applications, in fact, qsetting not only can save user settings, regardless of what variables can be saved, including user login status, although this may not be the best choice. When using qsetting, some of the contents of the Qvariant class are involved, so let's look at how to use them.
qsetting Initialize
Qsettings settings ("Company", "App");
If you use qsetting in multiple places, you should set this up:
Qcoreapplication::setorganizationname ("Company");
Qcoreapplication::setorganizationdomain ("company.com");
Qcoreapplication::setapplicationname ("App");
...
Qsettings settings;
In OS X, use domains instead of name to identify the software (if you do not set up domain, the program will infer domain based on name) storage
void Qsettings::setvalue (const QString & key, const qvariant & value)
Note: Modifying the original settings does not take effect immediately, you can use Sync () to commit changes to read
Qvariant qsettings::value (const QString & key, const qvariant & defaultvalue = qvariant ()) const
When key does not exist, returns Null (DefaultValue is not set) or returns DefaultValue. detection and deletion of existence
Detecting the existence of a key
BOOL Qsettings::contains (const QString & key) const
Delete a key
void Qsettings::remove (const QString & key)
View all key values
Qstringlist Qsettings::allkeys () const
Remove all keys
void Qsettings::clear ()
prefix
void Qsettings::begingroup (const QString & prefix)
void Qsettings::endgroup ()
Multiple SetValue () can be used between BeginGroup () and Endgroup ().
Qsetting also supports the array concept
int Qsettings::beginreadarray (const QString & prefix)
void Qsettings::beginwritearray (const QString & prefix, int size =-1)
fallback mechanism
Key value Lookup order (assuming the company is Mysoft, the application is star Runner)
1. A user-specific location for the Star Runner application
2. A user-specific location for all applications by Mysoft
3. A system-wide location for the Star Runner application
4. A system-wide location for all applications by Mysoft
Set Qsetting Object
Qsettings (format format, scope scope, const QString & Organization, const QString & application = QString (), Qobje CT * parent = 0)
Scope:qsettings::systemscope, Qsettings::userscope
Format:qsettings::iniformat, QSettings::NativeFormat store and restore GUI programs
void Mainwindow::writesettings ()
{
qsettings settings ("Moose Soft", "Clipper");
Settings.begingroup ("MainWindow");
Settings.setvalue ("Size", size ());
Settings.setvalue ("Pos", POS ());
Settings.endgroup ();
}
void Mainwindow::readsettings ()
{
qsettings settings ("Moose Soft", "Clipper");
Settings.begingroup ("MainWindow");
Resize (settings.value ("Size", qsize (+)). Tosize ());
Move (Settings.value ("Pos", Qpoint (+)). Topoint ());
Settings.endgroup ();
}
Mainwindow::mainwindow ()
{
...
Readsettings ();
}
void Mainwindow::closeevent (qcloseevent *event)
{
if (Userreallywantstoquit ()) {
writesettings ();
Event->accept ();
} else {
event->ignore ();
}
}
qvariant
Transformation
Qvariant does not provide conversions from the Qvariant type to Qcolor, Qimage, and Qpixmap types (but the latter can be converted to the former type), but you can use the following template function for transformation
T Qvariant::value () const
T Qvariantvalue (const qvariant & value)
Registered User Type
int Qregistermetatype (const char * typeName)
void qregistermetatypestreamoperators (const char * typeName)