QT : Templated Q_OBJECT class

后端 未结 4 758
予麋鹿
予麋鹿 2020-11-29 03:26

Is it possible to have a template class, which inherit from QObject (and has Q_OBJECT macro in it\'s declaration)?

I would like to create something like adapter for

4条回答
  •  忘掉有多难
    2020-11-29 04:04

    Taking into account some restrictions: you can. First please became familiar (if already not) https://doc.qt.io/archives/qq/qq16-dynamicqobject.html. - it will help to imlement it. And about restrictions: you can have a template QObject class i.e. template class derived from QObject, but:

    1. Do not tell the moc to compile it.
    2. Q_OBJECT is just a macro and you have to replace it by it real content which is virtual interface and something else :)
    3. Implement QMetaObject activation (above mentioned virtual interface and be caution with object info data, which is also come from Q_OBJECT) and some else functionality and you will have template QObject (even with template slots)
    4. But as I managed to catch the one draw back - it is not possible to simply use this class as a base for another class.
    5. There are some other drawbacks - but I think the detail investigation will show you them.

    Hope this will helpful.

提交回复
热议问题