0

Hi i am trying to write a template class in Qt,while doing so i was got stuck with some errors,i have read some articles and started to build an example according to my requirement

template.h

#ifndef CSLCDTEMPLATE_H
#define CSLCDTEMPLATE_H

#include <QDialog>
#include <QTimer>
#include <QDebug>
#include <QList>
#include <QPixmap>
#include <QPalette>
#include <QStringList>


template<class T>
class LcdTemplate : public QDialog
{
public:
    LcdTemplate();
    void display(T);
    T Getalue();

private slots:
    void display();
private:
    int indexVal;
    T m_Obj;
    QStringList nameList;
};

#endif
// CSLCDTEMPLATE_H

template.cpp

#include "CSLcdTemplate.h"

extern QStringList display_list;

template <class T>
LcdTemplate<T>::LcdTemplate()
{
    qDebug()<<"Inside the Constructor of LCD Template";

    setWindowFlags(Qt::FramelessWindowHint);
#ifdef GL11_QT
    setGeometry(0,0,320,240);
#endif
#ifdef GL11_GNOME
    setGeometry(2,20,316,200);
#endif
    setStyleSheet("background-color:yellow");

    indexVal = 0;

    QTimer *timer = new QTimer(this);
    connect(timer, SIGNAL(timeout()), this, SLOT(display()));
    timer->start(500);

    QTimer::singleShot(4000, this, SLOT(close()));
}

//template <class T>
void LcdTemplate::display()
{

    nameList = display_list;
    qDebug()<<"Data in"<<nameList;
    display(nameList);

}

template <class T>
void LcdTemplate<T>::display(T list)
{
    switch(indexVal)
    {
    case 0:
        this->setStyleSheet(nameList.at(indexVal));
        indexVal = 1;
        break;
    case 1:
        this->setStyleSheet(nameList.at(indexVal));
        indexVal = 2;
        break;
    case 2:
        this->setStyleSheet(nameList.at(indexVal));
        indexVal = 3;
        break;
    case 4:
        this->setStyleSheet(nameList.at(indexVal));
        indexVal = 4;
        break;
    case 5:
        this->setStyleSheet(nameList.at(indexVal));
        indexVal = 0;
        break;
    }
}

template <class T>
T TestTemp<T>::Getalue()
{
   return m_Obj;
}

The errors i am facing are

CSLcdTemplate.cpp:29:6: error: 'template<class T> class LcdTemplate' used without template parameters
CSLcdTemplate.cpp: In function 'void display()':
CSLcdTemplate.cpp:32:5: error: 'nameList' was not declared in this scope
CSLcdTemplate.cpp: At global scope:
CSLcdTemplate.cpp:67:11: error: expected initializer before '<' token

How can i resolve this errors.

Mounika
  • 1
  • 3
  • Template implementation in a cpp file??? Have a look at [here](https://isocpp.org/wiki/faq/templates#templates-defn-vs-decl). Yet, there are some limited cases you still can do this, but in general you will fail. – Aconcagua Mar 22 '17 at 09:56

1 Answers1

0

Do not mix the template class and the class responsible for signals and slots. See QT : Templated Q_OBJECT class

Note that the correct syntax for template class member definition is

template <class T>
void LcdTemplate<T>::display()
{}

Note that you need to add the Q_OBJECT macro as well for signal and slots to work.

Community
  • 1
  • 1
UmNyobe
  • 22,539
  • 9
  • 61
  • 90
  • I thought it's not possible to create a QObject that's a template. Did that change recently or something? – The Quantum Physicist Mar 22 '17 at 10:03
  • I too read in some articles while searching for the solution,so i am trying to build the example using inheritance but i was unable to achieve using inheritance too.Looking for more elaborated examples. – Mounika Mar 22 '17 at 10:15
  • @Mounika don't do it. Can you elicit why you think you need inheritance?? – UmNyobe Mar 22 '17 at 10:38
  • I have read in some articles that we can't directly use Q_OBJECT in a template class.In order to resolve i have rewritten my code by writing all signals and slots in a class and derived the template class from signals and slots class and overridden the methods.It would be better if you can look at my code.how can post the new code..? – Mounika Mar 22 '17 at 10:56
  • If you override the methods, and you need these methods to be signals or slots, you NEED the Q_OBJECT in the derived class. Which means you are back to square one. I suggest you to use composition instead of inheritance. – UmNyobe Mar 22 '17 at 11:09