2017-03-22 21 views
0

中的模板问题您好,我正在尝试在Qt中编写一个模板类,同时这样做我被卡住了一些错误,我已经阅读了一些文章并开始根据我的要求构建示例如何解决Qt

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; 
} 

我面对的错误是

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 

我该如何解决这个错误。

+0

cpp文件中的模板实现?看看[这里](https://isocpp.org/wiki/faq/templates#templates-defn-vs-decl)。然而,有些情况下你仍然可以做到这一点,但总的来说你会失败。 – Aconcagua

回答

0

不要混合模板类和负责信号和插槽的类。见QT : Templated Q_OBJECT class

注意,对于模板类成员定义正确的语法是

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

请注意,您需要添加Q_OBJECT宏,以及用于信号和槽工作。

+0

我认为不可能创建一个模板的QObject。最近有没有改变? –

+0

我也在阅读一些文章的同时寻找解决方案,所以我试图使用继承来构建示例,但我无法实现使用继承too.Looking更详细的示例。 – Mounika

+0

@Mounika不要这样做。你能否引出你认为你需要继承的原因? – UmNyobe