[Interest] How to create QObject-derived class instance from class name

Thiago Macieira thiago.macieira at intel.com
Tue Mar 1 16:30:19 CET 2016


On terça-feira, 1 de março de 2016 13:59:07 PST André Somers wrote:
> I think you did not get the point about QMetaType and QMetaObject. They
> _can_ be used together:

I don't think that they can.

> 1) mark the constructors in the classes you want to be able to construct
> from your file as Q_INVOKABLE
> 2) register the types in the meta object system

The meta *object* system has no registration.

The meta *type* system requires that the registered type be default-
constructible and copyable, but QObject is not copyable. Therefore, QObject-
derived classes cannot be registered with the meta type system.

> However, I think you are probably better off just using a factory pattern.

Correct, that's what the OP needs. Create your own list of classes and how to 
create them.

You can use a list of QMetaObject by way of the invocable constructor. But you 
can also implement the factory by directly calling the constructors yourself.

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




More information about the Interest mailing list