我试图制作读取 xml 文件并从此 xml 反序列化各种 qt 控件的代码,并且我使用 QDomDocument 执行此操作,并且我想从我的反序列化方法中获取 QLlist。我遇到了一些麻烦,这是模板类(.h)文件的一些代码:
QList<T*> deserialize(QIODevice *input)
{
QList<T*> objects = QList<T*>();
if(_deserializeObject(input, objects))
return objects;
}
bool _deserializeObjects(QIODevice* input, QList<QObject*>& list);
和我的 .cpp 文件与反序列化方法,这里我从文件中读取控制标签:
bool Serializer::_deserializeObjects(QIODevice* input, QList<QObject *> &objects)
{
QDomDocument doc;
if (!doc.setContent(input))
return false;
QDomElement root= doc.documentElement();
for(int j = 0; j < root.childNodes().length();j++)
{
QObject* object;
qDebug() << root.tagName();
if(root.tagName().contains("QGroupBox")) // <------- Here i need to determine which control i need to process.
{
????
}
qDebug () << object->metaObject()->className();
qDebug() << object->metaObject()->propertyCount();
for(int i = 0; i < object->metaObject()->propertyCount(); i++)
{
object->metaObject()->cast()
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();
if(propName == "x")
{
x = v.toInt();
}
else if(propName == "y")
{
y = v.toInt();
}
else if(propName == "width")
{
width = v.toInt();
}
else if(propName == "height")
{
height = v.toInt();
}
if(propName == "geometry")
{
continue;
}
object->setProperty(propName.toLatin1().data(), QVariant(v));
}
object->setProperty("geometry",QVariant(QRect(x,y,width,height)));
objects.push_back(object);
}
return true;
}
在这部分
if(root.tagName().contains("QGroupBox")) // <------- Here i need to determine which control i need to process.
{
????
}
qDebug () << object->metaObject()->className();
qDebug() << object->metaObject()->propertyCount();
for(int i = 0; i < object->metaObject()->propertyCount(); i++)
{
...
}
我想实际上以某种方式按名称获取控件的类型,所以问题是,我可以将 QGroupBox 转换为 QObject 保存 QGroupBox 属性,以便 QObject 元对象类名是 QGroupBox,所以我可以传递所有这些属性吗?因为我不想为每种控件类型制作循环。当我得到这样的结果时,我也是:
QList<QObject *> ds = s.deserialize<Object>((QIODevice*)&f);
然后我可以在一个循环中传递所有 QObjects 并使用 QMetaObject 类名并使用 qobject_cast 将每个对象强制转换为 QPushButton、QLabel 等吗?