QApplication app(argc, argv);
app.setApplicationName(app.translate("main", "Timelog"));
app.setOrganizationName("Qtrac Ltd.");
app.setOrganizationDomain("qtrac.eu");
//QSettings构造函数为空可以使用app.setOrganizationName(...)中的名字构造,避免每次
构造QSettings都要传入命名。
const QString FilenameSetting("Filename");
const QString GeometrySetting("Geometry");
QSettings settings;
settings.setValue(GeometrySetting, saveGeometry());
settings.setValue(FilenameSetting, model->filename());
QSettings settings;
restoreGeometry(settings.value(GeometrySetting).toByteArray());
QString filename = settings.value(FilenameSetting).toString();
2、配置文件读写
/**
* @brief 记录用户操作到ini文件
*
* @param log_key 键
* @param log_value 记录文件的值
* @param group 记录的组
*/
template<typename T>
void writeIniLog(const QString &log_key,
const T &log_value,
const QString &group = c_sConfig)
{
QSettings *config = new QSettings(getGCZSLogPath() +
c_optionLogInfoFile, QSettings::IniFormat);
config->beginGroup(group);
config->setValue(log_key,log_value);
config->endGroup();
delete config;
}
/**
* @brief 读取配置文件值
*
* @fn readIniLog
* @param log_key
* @param group
* @return QVariant
*/
QVariant readIniLog(const QString &log_key, const QString &group = c_sConfig)
{
QString sOptionFile = getGCZSLogPath() + c_optionLogInfoFile;
QVariant log_value;
QSettings *config = new QSettings(sOptionFile, QSettings::IniFormat);
log_value = config->value(group + "/" + log_key);
delete config;
return log_value;
}