Registering types vs containers

445 views Asked by At

If I register a custom type like the following, for the use in queued connections:

Q_DECLARE_METATYPE(MyClass);
qRegisterMetaType<MyClass>();

I can use the type in queued connection with signals like this one:

void MySignal(MyType o);

Now I also would like to use the type with signals like this:

void MyVectorSignal(QVector<MyType> v);

I remember I read somewhere that Qt automatically allows using registered types with containers without explicitely registering the specific type/container combination.

But it didn't work for me. I had to register the container as well:

Q_DECLARE_METATYPE(QVector<MyType>);
qRegisterMetaType<QVector<MyType>>();

Should it work without the latter registration code? Is this actually necessary? Should containers work out of the box?

1

There are 1 answers

0
demonplus On

From Qt docs for Q_DECLARE_METATYPE:

Some types are registered automatically and do not need this macro:

  • Pointers to classes derived from QObject
  • QList, QVector, QQueue, QStack, QSet or QLinkedList where T is a registered meta type
  • QHash, QMap or QPair where T1 and T2 are registered meta types
  • QPointer, QSharedPointer, QWeakPointer, where T is a class that derives from QObject
  • Enumerations registered with Q_ENUM or Q_FLAG
  • Classes that have a Q_GADGET macro

So you don't need to register QVector<T>. Just make sure T is a registered meta type.