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

时间:2017-02-01 18:56:03

标签: c++ qt serialization qmetaobject

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

代码被简化为更易读,但主要思想已经到位。

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 );
    }
};

1 个答案:

答案 0 :(得分:1)

解决方案是使用构造函数

扩展模板
SettingsItem()
{
    qRegisterMetaType<T>();
    qRegisterMetaTypeStreamOperators<T>(T::staticMetaObject.className());
}

并在类构造函数中注册嵌套类型

RegistersSettings()
{
    qRegisterMetaTypeStreamOperators<QList<RegisterGroupSettings>>("QList<RegisterGroupSettings>");
}