c++ - 如何将自定义类型保存到 QSettings?

121 篇文章 12 订阅

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

Test Results Linux      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();

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值