2017-03-22 8 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; 
} 

내가 직면하고 오류가

01 아르

어떻게이 오류를 해결할 수 있습니까?

+0

cpp 파일의 템플릿 구현 ??? [여기] (https://isocpp.org/wiki/faq/templates#templates-defn-vs-decl)를보십시오. 그러나 여전히 제한된 경우가 있지만 일반적으로 실패 할 수 있습니다. – Aconcagua

답변

0

템플릿 클래스와 신호 및 슬롯을 담당하는 클래스를 혼합하지 마십시오. 템플릿 클래스 멤버 정의에 대한 올바른 구문은 당신이 일을하는 신호와 슬롯뿐만 아니라 Q_OBJECT 매크로를 추가해야

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

참고 것을 QT : Templated Q_OBJECT class

참고를 참조하십시오.

+0

QObject를 템플릿으로 만들 수 없다고 생각했습니다. 최근에 그런 변화가 있었습니까? –

+0

나는 솔루션을 검색하는 동안 일부 기사를 읽었으므로 상속을 사용하여 예제를 작성하려고하지만 상속도 사용하지 못했습니다.보다 정교한 예제를 살펴보십시오. – Mounika

+0

@ 모 니카는하지 않습니다. 왜 상속이 필요하다고 생각 하는지를 알 수 있습니까 ?? – UmNyobe