Qt覆盖控件deserealization上的类属性

时间:2013-08-17 21:30:12

标签: c++ qt class properties deserialization

我试图进行简单的控制serealization / deserialization,我想出了以下代码(.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子类化并将这些属性公开?我如何在子类中覆盖这些属性?

1 个答案:

答案 0 :(得分:2)

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

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

另见Qt's Property System

例如,属性xywidthheight不属于writable,但分别属于pos的一部分}和size属性,writable,但非stored,因为它们本身就是较大属性geometry的一部分,这是您真正需要保存的唯一属性并恢复,您可以检查此属性是stored还是writable