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

Ch'Gans chgans at gna.org
Thu Feb 23 00:37:16 CET 2017


On 23 February 2017 at 05:42, Thiago Macieira <thiago.macieira at intel.com> wrote:
> 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.

Ok, makes sense. No black magic here.

[...]
>
>> >> 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.

OK, I take it as "Undefined Behaviour", since you mentioned this above.
As my code works, i'll keep it for now (not mission-critical), but i
take note that QList<A*> has nothing to do with QList<B*> from a type
(and QVariant) point of view, whatever the relationship is between A
and B.

In the long run, i will use a factory approach, and fully delegate
Object* management to the factory.

Thanks a lot for sharing on the technical "details".

Chris

>
> --
> Thiago Macieira - thiago.macieira (AT) intel.com
>   Software Architect - Intel Open Source Technology Center
>
> _______________________________________________
> Interest mailing list
> Interest at qt-project.org
> http://lists.qt-project.org/mailman/listinfo/interest



More information about the Interest mailing list