February 2, 2012

aekam aekam
Ant Farmer
87 posts

template class not supported by Q_OBJECT

 

  1. // -- header file -------------------------------------------
  2. #ifndef MY_CLASS_C_H
  3. #define MY_CLASS_C_H
  4.  
  5. #include <QObject>
  6.  
  7. template <typename MY_TYPE>
  8. class MY_CLASS_c : public QObject
  9. {
  10.     Q_OBJECT
  11.  
  12. public:
  13.     explicit MY_CLASS_c(QObject *parent = 0);
  14.  
  15. signals:
  16.  
  17. public slots:
  18.  
  19. };
  20.  
  21. #endif // MY_CLASS_C_H
  22.  
  23. // -- source file --------------------------------------------
  24. #include "my_class_c.h"
  25.  
  26. template <typename MY_TYPE>
  27. MY_CLASS_c <MY_TYPE>::MY_CLASS_c(QObject *parent) :
  28.     QObject(parent)
  29. {
  30. }

  1. // -- compiler output ---------------------------------
  2. 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 [developer.qt.nokia.com]

 Signature 

If you take care of inches, you won’t have to worry about miles… :)

8 replies

February 2, 2012

koahnig koahnig
Mad Scientist
2112 posts

welcome to devnet

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

February 2, 2012

aekam aekam
Ant Farmer
87 posts

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

 Signature 

If you take care of inches, you won’t have to worry about miles… :)

February 2, 2012

aekam aekam
Ant Farmer
87 posts

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.???

 Signature 

If you take care of inches, you won’t have to worry about miles… :)

February 2, 2012

Andre Andre
Area 51 Engineer
6031 posts

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

 Signature 

Looking for Qt developers to join our team @ i-Optics: https://qt-project.org/forums/viewthread/25393/

February 2, 2012

Asperamanca Asperamanca
Hobby Entomologist
373 posts

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.

February 3, 2012

aekam aekam
Ant Farmer
87 posts

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.???

 Signature 

If you take care of inches, you won’t have to worry about miles… :)

February 3, 2012

Andre Andre
Area 51 Engineer
6031 posts

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.

 Signature 

Looking for Qt developers to join our team @ i-Optics: https://qt-project.org/forums/viewthread/25393/

February 3, 2012

aekam aekam
Ant Farmer
87 posts

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

 Signature 

If you take care of inches, you won’t have to worry about miles… :)

 
  ‹‹ how to get executable of a Qt project inorder to run it different machine?      [SOLVED] QUdpSocket and SO_BROADCAST ››

You must log in to post a reply. Not a member yet? Register here!