[Interest] Creating a QVariant of QList<QObject*> knowing only the class name

Ch'Gans chgans at gna.org
Wed Feb 22 03:20:50 CET 2017


Hi there,

I can create a valid QVariant that contains a MyObject pointer with
just the class name (ie, QString("MyObject")) and no access to the
class declaration. MyObject is derived from QObject.

This QVariant has the following properties:
- type() == QVariant::UserType
- userType() == QMetaType::type("MyObject*")

The consumer of this variant, which has access to the original class
declaration, can obtain a valid pointer with
variant.value<MyObject*>()

Now I need to create a QVariant that contains a QList<MyObject*> with
just the class name (ie, QString("MyObject")) and no access to the
class declaration.

Again the resulting variant needs to have the following properties:
- type() == QVariant::UserType
- userType() == QMetaType::type("QList<MyObject*>")

My question is how do I create and "populate" such a variant without
using template involving MyObject*?
QSequentialIterable allows one to iterate on such a variant, but only
provide a const access, so it is useless in my case.

I seem to have found a solution, but i'm not sure it is correct and safe:
QList<QObject*> list;
// populate the list with MyObject* items
int desiredMetaTypeId = QMetaType::type("QList<MyObject*>")
return QVariant(desiredMetaTypeId, &list);

Does anyone know a "proper" solution for this? Or is my approach
correct and safe?

Thanks in advance,
Chris



More information about the Interest mailing list