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