读/写QObject [英] Reading/writing QObjects
本文介绍了读/写QObject的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我认为我可以使用 Q_PROPERTY
这样写一个 QObject
> s:
QDataStream& operator<<<(QDataStream& ds,const Object& obj){
for(int i = 0; i if(obj.metaObject() - > property(i).isStored(& obj)){
ds< obj.metaObject() - > property(i).read(& obj);
}
}
return ds;
}
如果是这样,我不知道为什么 QObject
s尚未实现该方法,因为它非常通用。但这是除了这一点。我如何读取文件?即,实现此函数?
QDataStream& operator>>(QDataStream& ds,Object& obj){
return ds;
}
我想我可以用 ds。
对象
是继承自 QObject
的自定义类。解决方案
这似乎工作。
QDataStream& operator> ; ds,Object& obj){
QVariant var;
for(int i = 0; i if(obj.metaObject() - > property(i).isStored (& obj)){
ds>> var;
obj.metaObject() - > property(i).write(& obj,var);
}
}
return ds;
}
感谢 Eugene 。
I think I can write a QObject
like this by taking advantage of the Q_PROPERTY
s:
QDataStream &operator<<(QDataStream &ds, const Object &obj) {
for(int i=0; i<obj.metaObject()->propertyCount(); ++i) {
if(obj.metaObject()->property(i).isStored(&obj)) {
ds << obj.metaObject()->property(i).read(&obj);
}
}
return ds;
}
Which, if that's true, I don't know why QObject
s don't already have that method implemented because it's pretty generic. But that's besides the point. How would I read the file? i.e., implement this function?
QDataStream &operator>>(QDataStream &ds, Object &obj) {
return ds;
}
I'm thinking I can somehow use ds.readBytes
but how would I get the length of the property?
PS: If it wasn't obvious, Object
is my custom class that inherits from QObject
.
解决方案
This seems to work.
QDataStream &operator>>(QDataStream &ds, Object &obj) {
QVariant var;
for(int i=0; i<obj.metaObject()->propertyCount(); ++i) {
if(obj.metaObject()->property(i).isStored(&obj)) {
ds >> var;
obj.metaObject()->property(i).write(&obj, var);
}
}
return ds;
}
Thanks to Eugene.
这篇关于读/写QObject的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文