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

Thiago Macieira thiago.macieira at intel.com
Wed Feb 22 17:42:13 CET 2017


On quarta-feira, 22 de fevereiro de 2017 02:40:37 PST Ch'Gans wrote:
> > If you're going to call the QVariant constructor with a pointer, you MUST
> > pass a pointer to the correct, concrete object. Forget that it was a
> > template: just
> Well that's my problem, i cannot instanciate and assign the instance
> to a variable of the concrete type since i don't have access to the
> class declaration, not even to a forward declaration.

That seals your decision. If you don't have a pointer to an object of type 
MyObjectList, then your QVariant can only contain be of one value: a null 
QVariant.

> > think of it as "MyObjectList" that has absolutely no relationship with
> > QObjectList.
> 
> I understand that, i got caught when i tried to handle QVariant
> holding a QStringList as a QVariant holding a QList<QString>...

That's different. QStringList derives from QList<QString>, so there's an "is-
a" relationship. For Qt 6, I want to make QStringList be a typedef to 
QList<QString>.

There's no such relationship in your case. MyObjectList and QObjectList have 
no relationship whatsoever: they are completely disparate and separate 
classes.

> >> Does anyone know a "proper" solution for this? Or is my approach
> >> correct and safe?
> > 
> > Your approach is to redesign and think the problem from another angle.
> 
> Well, what is the point of Qt meta-type/object then? Is it an abuse to
> do introspection and dynamic creation? Doesn't Qt (to some extent)
> brings to C++ what Java and C# provides natively?

The point of the metatype system is to like std::any. It is a simple way of 
registering functions to register in a central database a set of functions to 
manipulate types.

The metaobject system includes reflection. The metatype system does not.

Neither abuse anthing. They're fully within the language. So yes, Qt tries to 
bring some niceties of other languages into C++, but its hands are tied in 
some ends, where there's simply no solution. Despie what many say, Qt is still 
C++ and does not change any of the language rules.

> 'MyObject*' is registered to the QMetaType system, 'MyObject' class is
> 'registered' to the QMetaObject system. Qt can do "magic" stuff (C++
> <-> QML & JS), but i cannot do the same myself?

Sure you can. 

But there's a difference between magic and undefined behaviour. According to 
Clarke's Third Law, magic is undistinguishable from sufficiently advanced 
technology -- or, in our case, sufficiently advanced code/implementation. But it 
still remains within the boundaries of the language. The moment you step 
outside, everything falls apart.

That's what you asked to do in your code.

Now, you *can* implement what you want. You can create your own list type 
erasure database that permits manipulation (unlike QSequentialIterable). That 
way, you can ask it to create an empty list for you, add items, etc.

But that's not the metatype system. 

> This might work, but i'm trying to find a solution that wouldn't
> require to write lot of boiler-plate code, b/c with the above solution
> one will have to write boiler-plate code (create object, create list
> of objects, create map of objects, create set of objects, create hash
> of objects...) for every single object type...

And what is the metatype system besides boilerplate? It is smart about using 
templates and a single macro, but it's nothing different.

> Tanks for all your inputs, but I'm still puzzled, I can't believe this
> is not achievable in a generic way...

It is achievable. I'm saying that the Qt metatype system doesn't do what you 
want and that the way you tried was UB.

-- 
Thiago Macieira - thiago.macieira (AT) intel.com
  Software Architect - Intel Open Source Technology Center




More information about the Interest mailing list