本文先从概念和用法上讲述下QSettings的简单用法,最后以一个简单而较全面的使用例子结束,一句话:实践出真知!
QSettings 类提供“持久性”,平台独立的应用程序设置。意思是,程序关闭后QSettings把窗体大小,位置,用户设置的Option 保存起来,下次
打开程序时再重新加载。
如果你需要的是“非持久性” 基于内存的数据结构,可以考虑使用QMap<QString,QVariant> 代替。
通常,对于Windows,配置信息保存在“注册表”中,Mac OS 是XML 文件,Unix 是INI 文件
// 构造函数
QSettings settings("MySoft", "Star Runner");
QCoreApplication::setOrganizationName("MySoft");
QCoreApplication::setOrganizationDomain("mysoft.com");
QCoreApplication::setApplicationName("Star Runner");
...
QSettings settings;
如果要在程序很多地方使用QSettings,用 QCoreApplication::setOrganizationName() 和QCoreApplication::setApplicationName() 然后用QSettings 的默认构造函数。
这样,公司或组织名,应用程序名只要指定一次,QSettings settings 到处扔就行。
QSettings 存储了一对键值( Key / value ) ,Key 是一个QString,Value是一个QVariant
settings.setValue("editor/wrapMargin", 68);
添加键值的时侯,如果Key 已存在,则Value 被重写。
出于效率的原因,setValue 不会立既写入(如注册表,INI, XML), 要立既写入可以用sync() 函数。
int margin = settings.value("editor/wrapMargin").toInt();
通过Key,取回Value。如果指定的Key 不存在,QSettings 返回空的QVariant(转成整型是 0)。
// 类型转换
QSettings settings("MySoft", "Star Runner");
QColor color =settings.value("DataPump/bgcolor").value<QColor>();
// 通过"/" 分组
settings.setValue("mainwindow/size",win->size());
settings.setValue("mainwindow/fullScreen",win->isFullScreen());
settings.setValue("outputpanel/visible",panel->isVisible());
settings.beginGroup("mainwindow");
settings.setValue("size", win->size());
settings.setValue("fullScreen",win->isFullScreen());
settings.endGroup();
settings.beginGroup("outputpanel");
settings.endGroup();
添加分组,并链表化,读写分组
void QSettings :: beginWriteArray ( const QString & prefix , int size = - 1 )
Adds prefix to the current group and starts writing an array of size size.
If size is -1 (the default), it is automatically determined based on the indexes of the entries written.
If you have many occurrences of a certain set of keys,
you can use arrays to make your life easier.
For example, let's suppose that you want to save a variable-length list of user names and passwords.
You could then write:
structLogin{
QString userName;
QString password;
};
QList<Login> logins;
...
QSettings settings;
settings.beginWriteArray("logins");
for (int i = 0; i < logins.size(); ++i) {
settings.setArrayIndex(i);
settings.setValue("userName", list.at(i).userName);
settings.setValue("password", list.at(i).password);
}
settings.endArray();
遍历分组
intQSettings::beginReadArray(constQString&prefix)
Adds prefix to the current group and starts reading from an array. Returns the size of the array.
Example:
struct Login {
QString userName;
QString password;
};
QList<Login> logins;
...
QSettings settings;
int size = settings.beginReadArray("logins");
for (int i = 0; i < size; ++i) {
settings.setArrayIndex(i);
Login login;
login.userName = settings.value("userName").toString();
login.password = settings.value("password").toString();
logins.append(login);
}
settings.endArray();
///MailClient的例子/
void MailClient::readSettings()
{
QSettings settings("Software Inc.", "Mail Client");
settings.beginGroup("mainWindow");
restoreGeometry(settings.value("geometry").toByteArray());
mainSplitter->restoreState(
settings.value("mainSplitter").toByteArray());
rightSplitter->restoreState(
settings.value("rightSplitter").toByteArray());
settings.endGroup();
}
void MailClient::writeSettings()
{
QSettings settings("Software Inc.", "Mail Client");
settings.beginGroup("mainWindow");
settings.setValue("geometry", saveGeometry());
settings.setValue("mainSplitter", mainSplitter->saveState());
settings.setValue("rightSplitter", rightSplitter->saveState());
settings.endGroup();
}