gpt4 book ai didi

c++ - Qt Override class properties on control deserealization

转载 作者:行者123 更新时间:2023-11-30 04:15:25 25 4
gpt4 key购买 nike

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

最佳答案

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

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

另见 Qt's Property System .

例如,属性xywidthheight是不可可写的,但分别是 possize 属性的一部分,它们是可写,但非存储,因为它们本身是较大属性 geometry 的一部分,这是您真正需要保存和恢复的唯一属性,您可以检查该属性是否同时被存储可写

关于c++ - Qt Override class properties on control deserealization,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18293517/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com