将(已存在的)QSetting 保存到 INI 文件中
我想将已经存在的 QSettings
对象保存到某个 INI 文件中以进行备份。
QSettings
来自应用程序的全局设置,即。它可以是注册表、ini 文件等。
如果有帮助,我的上下文是:
class Params
{
// All params as data members
// ...
void loadGlobal ()
{
Qettings s; // Global parameters, paths set by application
// Fill data members: s.value (...);
}
};
class Algo
{
Result run (Params p)
{
Result r = F(p);
return r;
}
};
int main (...)
{
Params p;
p.loadGlobal ();
Algo a;
Result r = a.run (p);
// At this point, save Result and Params into a specific directory
// Is there a way to do:
p.saveToIni ("myparams.ini"); // <-- WRONG
}
解决方案是将 saveTo (QSetting & s)
方法添加到 Params
class:
class Params
{
void saveTo (QSettings & s)
{
s.setValue (...);
}
};
int main (...)
{
Params p;
p.loadGlobal ();
QSettings bak ("myparams.ini", ...);
p.saveTo (bak);
}
但我正在寻找一种不修改 Params
类的解决方案。
I want to save an alredy-existing QSettings
object into some INI file for backup.
The QSettings
comes from the application's global settings, ie. it can be registry, ini file, etc.
In case it helps, my context is:
class Params
{
// All params as data members
// ...
void loadGlobal ()
{
Qettings s; // Global parameters, paths set by application
// Fill data members: s.value (...);
}
};
class Algo
{
Result run (Params p)
{
Result r = F(p);
return r;
}
};
int main (...)
{
Params p;
p.loadGlobal ();
Algo a;
Result r = a.run (p);
// At this point, save Result and Params into a specific directory
// Is there a way to do:
p.saveToIni ("myparams.ini"); // <-- WRONG
}
A solution would be to add a saveTo (QSetting & s)
method into the Params
class:
class Params
{
void saveTo (QSettings & s)
{
s.setValue (...);
}
};
int main (...)
{
Params p;
p.loadGlobal ();
QSettings bak ("myparams.ini", ...);
p.saveTo (bak);
}
But I am looking for a solution without modifying the Params
class.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
嗯,不,QT 并不真正直接支持这一点。我认为你最好的选择是编写一个辅助类......类似于:
Well, no, QT Doesn't really support this directly. I think your best bet is writing a helper class...something like:
我认为有两个问题:
如果您可以不完全控制结果路径,那么这应该足够了。如果没有,您仍然可以执行上述操作,然后使用 fileName() 获取文件名,并使用系统调用将文件复制/移动到所需的最终位置。
I think there are 2 issues:
If you're OK not having complete control over the resulting path, this should be sufficient. If not, you could still do the above, then get the file name using fileName() and use a system call to copy/move the file to the desired final location.