您现在的位置是:首页 > 电脑 > 

具有未知列表模板参数的QVariant到QList(QVariant to QList with unkown list template parameter)

2025-07-21 18:52:05
具有未知列表模板参数的QVariant到QList(QVariant to QList with unkown list template parameter) 我有几个QObject类,它们可能有QList属性,它们存储QObject派生类的列表: class Object : public QObject { Q_OBJECT Q_PRO
具有未知列表模板参数的QVariant到QList(QVariant to QList with unkown list template parameter)

我有几个QObject类,它们可能有QList属性,它们存储QObject派生类的列表:

class Object : public QObject
{
    Q_OBJECT
    Q_PROPERTY(QList<AnotherQObject*> otherObjects READ otherObjects)
public:
    QList<AnotherQObject*> otherObjects() ct;
}
 

QList<AnotherObject*>当然已在Q_DECLARE_METATYPE和qRegisterMetaType()注册。

现在我想用Qt的元对象系统读取这个属性。 在我想要读取列表的那一点上,我只关心(并且知道)列表包含指向对象的指针这一事实,这些对象是从QObject派生的,但不是精确的类型。

当我将属性的QVariant转换为QList<QObject *> ,列表为空。

QObject *object = getObjectFromSomeWhere();
ct char* propertyame = getPropertyameFromSomeWhere(); // "otherObjects"

QVariant list = object->property(name);
QList<QObject *> objectList = list.value<QList<QObject *> >(); // empty

QList<AnotherQObject*> anotherObjectList = 
    static_cast<Object*>(object)->otherObjects() // not empty
 

是否可以访问此列表? 你有其他建议吗?

编辑:虽然我发布了我的解决方案,我仍然喜欢评论:-)

I have several QObject classes, which may have QList properties, which store lists of QObject derived classes:

class Object : public QObject
{
    Q_OBJECT
    Q_PROPERTY(QList<AnotherQObject*> otherObjects READ otherObjects)
public:
    QList<AnotherQObject*> otherObjects() ct;
}
 

QList<AnotherObject*> has of course been registered with Q_DECLARE_METATYPE and qRegisterMetaType().

ow I want to read this property with Qt's meta object system. At the point where I want to read the list, I only care about (and know) the fact that the list contains pointers to objects, which have been derived from QObject, but not the exact type.

When I convert the property's QVariant to QList<QObject *>the list is empty.

QObject *object = getObjectFromSomeWhere();
ct char* propertyame = getPropertyameFromSomeWhere(); // "otherObjects"

QVariant list = object->property(name);
QList<QObject *> objectList = list.value<QList<QObject *> >(); // empty

QList<AnotherQObject*> anotherObjectList = 
    static_cast<Object*>(object)->otherObjects() // not empty
 

Is it possible to access this list? Do you have other suggesti?

EDIT: Although I posted my solution, I would still love comments :-)

最满意答案

如果您使用的是Qt 5.2或更高版本,则可以使用QSequentialIterable类,而无需额外模板,修改的宏和其他注册的开销。

http:///doc/qt-5/qsequentialiterable.html

如果QList<T*>存储在QVariant ,其中T是从QObject派生的,则可以从变量中获取QSequentialIterable实例,这允许您将列表中的每个项目作为QVariant进行迭代。 然后你可以简单地将每个项的值作为QObject* ,然后添加到QList<QObject*> (确保你测试了类型是从QObject派生的假设):

QList<QObject*> qobjectList; if ((QMetaType::QVariantList)) { QSequentialIterable iterable = variant.value<QSequentialIterable>(); foreach (ct QVariant& item, iterable) { QObject* object = item.value<QObject*>(); if (object) { qobjectList.append(object); } } }

If you're using Qt 5.2 or higher there is a QSequentialIterable class that allows you to do this without the overhead of extra templates, modified macros, and additional registrati.

http:///doc/qt-5/qsequentialiterable.html

If you have a QList<T*> stored in a QVariant, where T is derived from QObject, you can get a QSequentialIterable instance from the variant, which allows you to iterate over each item in the list as a QVariant. Then you can simply get the value of each item as a QObject*, adding to a QList<QObject*> as you go (make sure you test the assumption that the type is derived from QObject, though):

QList<QObject*> qobjectList; if ((QMetaType::QVariantList)) { QSequentialIterable iterable = variant.value<QSequentialIterable>(); foreach (ct QVariant& item, iterable) { QObject* object = item.value<QObject*>(); if (object) { qobjectList.append(object); } } }

#感谢您对电脑配置推荐网 - 最新i3 i5 i7组装电脑配置单推荐报价格的认可,转载请说明来源于"电脑配置推荐网 - 最新i3 i5 i7组装电脑配置单推荐报价格

本文地址:http://www.dnpztj.cn/diannao/65541.html

相关标签:无
上传时间: 2023-04-22 19:30:48
留言与评论(共有 7 条评论)
本站网友 梦想世界表情
2分钟前 发表
-) 最满意答案 如果您使用的是Qt 5.2或更高版本
本站网友 李冬梅
22分钟前 发表
-) I have several QObject classes
本站网友 怀念过去的日子
7分钟前 发表
则可以从变量中获取QSequentialIterable实例
本站网友 三亚二手房
23分钟前 发表
我仍然喜欢评论
本站网友 广州番禺租房
13分钟前 发表
Although I posted my solution
本站网友 过往矫正
14分钟前 发表
-) 最满意答案 如果您使用的是Qt 5.2或更高版本