1

我试图做一个简单的控制序列化/反序列化,我想出了以下代码(.cpp)

#include "serializer.h"
#include "QMetaProperty"
#include "QTextStream"
#include "QDebug"

Serializer::Serializer(QObject *parent) :
    QObject(parent)
{
}

bool Serializer::Serialize(QObject *object,QString name) {
 QDomDocument doc;
 QDomElement root = doc.createElement(object->metaObject()->className());
 doc.appendChild(root);

 for(int i = 0; i < object->metaObject()->propertyCount(); i++)
 {
  QMetaProperty prop = object->metaObject()->property(i);
  QString propName = prop.name();
  if(propName == "objectName")
   continue;
  QDomElement el = doc.createElement(propName);
  QVariant value = object->property(propName.toLatin1().data());
  QDomText txt;
  if (object->metaObject()->property(i).typeName() != "QByteArray") {
   txt = doc.createTextNode( value.toString() );
  } else {
   txt = doc.createTextNode(object->metaObject()->property(i).typeName());
  }
   el.appendChild(txt);
   root.appendChild(el);
 }


  QFile f(name);
  f.open(QIODevice::ReadWrite | QIODevice::Text);
  QTextStream stream(&f);
  doc.save(stream, 2);
  f.close();
  return true;
}

bool Serializer::_deserializeObject(QIODevice* input, QObject* object)
{
    QDomDocument doc;
    if (!doc.setContent(input))
        return false;
    QDomElement root = doc.documentElement();
    qDebug() << object->metaObject()->propertyCount();
    for(int i = 0; i < object->metaObject()->propertyCount(); i++)
    {
        QMetaProperty prop = object->metaObject()->property(i);
        QString propName = prop.name();
        if(propName == "objectName")
            continue;
        QDomNodeList nodeList = root.elementsByTagName(propName);
        if(nodeList.length() < 1)
            continue;
        QDomNode node = nodeList.at(0);
        QVariant value = object->property(propName.toLatin1().data());
        QString v = node.toElement().text();

        object->setProperty(propName.toLatin1().data(), QVariant(v));
    }
    return true;
}

.h 文件:

#ifndef SERIALIZER_H
#define SERIALIZER_H

#include <QObject>
#include <QDomDocument>
#include <QFile>

class Serializer : public QObject
{
    Q_OBJECT
public:
    explicit Serializer(QObject *parent = 0);
    bool Serialize(QObject *object, QString name);
    template<class T>
    T* deserialize(QIODevice *input)
    {
        T* object = new T();
        if(_deserializeObject(input, object))
            return object;
        delete object;
        return NULL;
    }


    bool _deserializeObject(QIODevice* input, QObject* object);
signals:

public slots:

};

#endif // SERIALIZER_H

所以在我序列化一些按钮之后,我想反序列化它的xml文件,我这样做:

QFile f(fname);
f.open(QIODevice::ReadOnly);

QPushButton *ds = s.deserialize<QPushButton>(&f);
f.close();

调试输出在转换时给了我很多属性错误

setProperty: Property "modal" invalid, read-only or does not exist
setProperty: Property "frameGeometry" invalid, read-only or does not exist
setProperty: Property "normalGeometry" invalid, read-only or does not exist
setProperty: Property "x" invalid, read-only or does not exist
setProperty: Property "y" invalid, read-only or does not exist
setProperty: Property "frameSize" invalid, read-only or does not exist
setProperty: Property "width" invalid, read-only or does not exist
setProperty: Property "height" invalid, read-only or does not exist
setProperty: Property "rect" invalid, read-only or does not exist
setProperty: Property "childrenRect" invalid, read-only or does not exist
setProperty: Property "childrenRegion" invalid, read-only or does not exist
setProperty: Property "isActiveWindow" invalid, read-only or does not exist
setProperty: Property "focus" invalid, read-only or does not exist

所以我的问题是,有没有办法以某种方式进行正确的转换,或者我应该继承 QPushButton 并公开这些属性?如何在我的子类中覆盖这些属性?

4

1 回答 1

2

尝试保存所有属性是没有意义的。

QMetaProperty有很多标志可以帮助您过滤应保存的属性(应避免使用非“存储”和非“可写”属性)。

另请参阅Qt 的属性系统

例如,属性xy是非,width但分别是和属性的一部分,它们是,但不是,因为它们本身是较大属性的一部分,这是您真正需要保存和恢复的唯一属性,您可以检查该属性是否为and 。heightwritablepossizewritablestoredgeometrystoredwritable

于 2013-08-18T00:32:51.853 回答