Template class not supported by Q_OBJECT



  • @
    // -- header file -------------------------------------------
    #ifndef MY_CLASS_C_H
    #define MY_CLASS_C_H

    #include <QObject>

    template <typename MY_TYPE>
    class MY_CLASS_c : public QObject
    {
    Q_OBJECT

    public:
    explicit MY_CLASS_c(QObject *parent = 0);

    signals:

    public slots:

    };

    #endif // MY_CLASS_C_H

    // -- source file --------------------------------------------
    #include "my_class_c.h"

    template <typename MY_TYPE>
    MY_CLASS_c <MY_TYPE>::MY_CLASS_c(QObject *parent) :
    QObject(parent)
    {
    }
    @

    @
    // -- compiler output ---------------------------------
    my_class_c.h:9: Error: Template classes not supported by Q_OBJECT
    @

    can anyone tell me where exactly i am making mistake.???

    [Edit] Please "use code wrapping tags ":http://developer.qt.nokia.com/wiki/ForumHelp#e3f82045ad0f480d3fb9e0ac2d58fb01


  • Moderators

    welcome to devnet

    You cannot combine templates and Q_OBJECT as the compiler output states.



  • I need to have signal-slot facility [Q_OBJECT] in a generic class [template]...
    any alternates.???



  • one solution is to create two different classes, one for signal slot mechanism with Q_OBJECT and one with template which contains generic functionalities...

    then to inherit them...

    any thing else other than this.???



  • Nope, that's your only hope.
    The issue is that moc cannot handle templated code.



  • As long as all you need to do is send and receive signals whose parameters are not of the templated type, the solution with a non-template base class, and a template derived class works well.



  • Ok, having base class with non template signal-slot functionality and to derive a template class from it, will work fine for me.

    but, just for knowledge, what if i need to have signal-slot with template type.???



  • Then, you are out of luck. The only thing you can do then, is wrap the type in a QVariant and emit the signal or define the slot based on the QVariant from the baseclass again. Or, you don't emit the actual value, and let clients call a normal getter method in response to a changed signal instead of sending the value directly. You simply cannot define signals and slots in a template class.



  • hmm... then calling getter function in response to the signal seems pretty good...


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.