c++ - 如何将自定义类型保存到 QSettings?
---------------------------------------------------------
QSettings settings(QSettings::IniFormat, QSettings::UserScope,"MySoft", "Star Runner");
执行结果:
C:\Users\abc\AppData\Roaming\MySoft\Star Runner.ini
-----------
Transport QVariant like QSettings does [solved] | Qt Forum
https://stackoverflow.com/questions/37333084/how-to-save-custom-type-to-qsettings
Transport QVariant like QSettings does [solved]
Transport QVariant like QSettings does [solved] | Qt Forum
--------------------------------------------------
相比之下 (simpleini): QSettings 的用法,太简单了。
在 linux OS上,虽然有排序方面的不足,但可以解决时间。
simpleini
https://github.com/brofield/simpleini
A cross-platform library that provides a simple API to read and write INI-style configuration files. It supports data files in ASCII, MBCS and Unicode. It is designed explicitly to be portable to any platform and has been tested on Windows, WinCE and Linux. Released as open-source and free using the MIT licence.
================================
c++ - 如何将自定义类型保存到 QSettings?
You have to implement streaming. TestClass
should have 2 overloaded operators <<, >>. For instance:
class TestClass
{
public:
QString testString;
qint32 testInt;
friend QDataStream & operator << (QDataStream &arch, const TestClass & object)
{
arch << object.testString;
arch << object.testInt;
return arch;
}friend QDataStream & operator >> (QDataStream &arch, TestClass & object)
{
arch >> object.testString;
arch >> object.testInt;
return arch;
}
};Q_DECLARE_METATYPE(TestClass)
Before saving instance of TestClass you have to use qRegisterMetaTypeStreamOperators function, like this:
qRegisterMetaTypeStreamOperators<TestClass>("TestClass");
QSettings settings(QSettings::IniFormat, QSettings::UserScope,"MySoft", "Star Runner");
settings.setValue("TestGroup/TestVal", QVariant::fromValue(test));
settings.sync();