PDA

View Full Version : signal/slots in template classes



killkolor
6th December 2006, 17:54
hi!

i have a couple of template classes and let them inherit from QObject to use the signal/slot observer pattern. at compile-time though i get a whole lot of linker errors. is it possible to use qt's signal/slot mechanism in template classes? if not, are there alternatives?
i'll give some samples out of my code and one of the linker errors to make things a bit clearer:



template <class T>
class ColladaReference :
public QObject
{
Q_OBJECT
public:
ColladaReference(){};
ColladaReference(T *ref){reference = ref;}
~ColladaReference(){};

void deleteReference();

private:
T *reference;
};
==========
template <class T>
class ColladaRepresentation :
public QObject
{
Q_OBJECT
public:
ColladaRepresentation(){representation = NULL;} // constructor
ColladaRepresentation(T *rep){representation = rep;} // constructor with collada ptr

void setRepresentation(T *rep){representation = rep;}
public slots:
void deleteRepresentation();
signals:
void representationDeleted(){};
private:
T * representation;
};
====================
typedef ColladaRepresentation<domGeometry> GeometryRepresentation;
typedef ColladaReference<domInstance_geometry> GeometryReference;
====================
GeometryReference *geometryReference = new GeometryReference(instanceGeometry);
GeometryRepresentation *geometryRepresentation = new GeometryRepresentation(geometryNode[k]);
connect(geometryRepresentation, SIGNAL(representationDeleted()), geometryReference, SLOT(deleteReference()));
=================

Error 3 error LNK2001: unresolved external symbol "public: virtual struct QMetaObject const * __thiscall ColladaReference<class domInstance_geometry>::metaObject(void)const " (?metaObject@?$ColladaReference@VdomInstance_geome try@@@@UBEPBUQMetaObject@@XZ) colladaLoader.obj

e8johan
6th December 2006, 18:18
Have you had a look at this article? http://doc.trolltech.com/qq/qq15-academic.html

Brandybuck
6th December 2006, 18:28
No, it's not generally possible to use signal/slots in template classes. That's because MOC/UI are pre-processors but the actual classes don't get generated until compile time. This is the one disadvantage of signal/slots I've run across.