c++qttemplatesqt5qmetaobject

Why is it not possible to use Q_OBJECT with template classes?


There's a similar question here that was looking for a workaround and a nice guide here which describes the meta-object system in Qt.But that doesn't explain directly why is it not possible to use a template class in the meta-object system.That's an annoying restriction.

Anyone know why ?


Solution

  • It is not possible* in standard Qt, as for Qt, as for these classes moc needs to be able to pre-process the class and generate the needed meta data structures. That is done before compilation, and for a template class that means that the type is not really defined yet.

    However, some things are possible:

    *) nothing is really impossible of course... If everything else fails, you can also implement everything moc provides yourself...