在 QT 中从 Q_PROPERTY 序列化嵌套用户定义的 class

Serialize nested user defined class from Q_PROPERTY in QT

面临以下问题:我无法从 Q_PROPERTY 序列化用户定义的对象 我尝试将 RegistersSettings class 序列化为 QDataStream。这个想法是能够将它序列化为文本文件(使用 << 运算符),然后能够读取它(使用 >> 运算符)。它应该验证从文件中读取的字段是否仍然有效。所以我为此检查了 属性。 问题是 Q_PROPERTY(QList groups MEMBER groups) 没有按预期工作。 看起来可以创建这样的功能,但看起来并不那么容易。 谁能帮助解决如何从 Q_PROPERTY 序列化用户定义的 class 的常用方法?

代码已简化以提高可读性,但主要思想已经到位。

class RegisterGroupSettings:SettingsItem<RegisterGroupSettings>
{
private:
    Q_GADGET
    Q_PROPERTY(QString name MEMBER name)
    Q_PROPERTY(int interval MEMBER interval)

public:
    QString name;
    int     interval;
};
Q_DECLARE_METATYPE(RegisterGroupSettings)

class RegistersSettings:SettingsItem<RegistersSettings>
{
private:
    Q_GADGET
    Q_PROPERTY(QList<RegisterGroupSettings> groups MEMBER groups)
    Q_PROPERTY(int code MEMBER code)

public:
    QList<RegisterGroupSettings> groups;
    int code;
};
Q_DECLARE_METATYPE(RegistersSettings)

SettingsItem 是统一的帮手

template <typename T> class SettingsItem
{
public:
    friend QDataStream & operator << (QDataStream &arch, const T & object)
    {
        const QMetaObject &mo = object.staticMetaObject;
        int cnt = mo.propertyCount();
        QString prop_name;
        QVariant prop_value;
        arch << cnt;
        while (cnt>0)
        {
            prop_name = mo.property(cnt-1).name();
            prop_value = mo.property(cnt-1).readOnGadget(&object);
            arch << prop_name;
            arch << prop_value;
            cnt--;
        }
        return arch;
    }

    friend QDataStream & operator >> (QDataStream &arch, T & object)
    {
        const QMetaObject &mo = object.staticMetaObject;
        int cnt=0;
        QString prop_name;
        QVariant prop_value;
        int prop_index;
        arch >> cnt;
        while (cnt>0)
        {
            arch >> prop_name;
            arch >> prop_value;
            prop_index = mo.indexOfProperty(prop_name.toStdString().c_str());
            if (prop_index > -1)
            {
                mo.property(prop_index).writeOnGadget(&object, prop_value);
            }
            cnt--;
        }
        return arch;
    }

    friend bool operator == (const T &first, const T &second)
    {
        const QMetaObject &mo = first.staticMetaObject;
        int cnt = mo.propertyCount();
        QString prop_name;
        QVariant oProp_value;
        QVariant dProp_value;
        while (cnt>0)
        {
            prop_name = mo.property(cnt-1).name();
            oProp_value = mo.property(cnt-1).readOnGadget(&first);
            dProp_value = mo.property(cnt-1).readOnGadget(&second);
            if (oProp_value == dProp_value)
            {
                cnt--;
                continue;
            }
            return false;
        }

        return true;
    }

    friend bool operator != (const T &first, const T &second)
    {
        return !( first == second );
    }
};

解决方案是使用构造函数扩展模板

SettingsItem()
{
    qRegisterMetaType<T>();
    qRegisterMetaTypeStreamOperators<T>(T::staticMetaObject.className());
}

并在class构造函数

中注册嵌套类型
RegistersSettings()
{
    qRegisterMetaTypeStreamOperators<QList<RegisterGroupSettings>>("QList<RegisterGroupSettings>");
}