如何解决Qt中的模板问题
How to resolve template issue in Qt
嗨,我
正在尝试用Qt编写一个模板类,在这样做时我遇到了一些错误,我已经阅读了一些文章并开始根据我的要求构建一个示例
模板.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
模板.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
如何解决此错误。
不要混合使用模板类和负责信号和插槽的类。请参阅 QT:模板化Q_OBJECT类
请注意,模板类成员定义的正确语法是
template <class T>
void LcdTemplate<T>::display()
{}
请注意,您还需要添加Q_OBJECT宏才能使信号和插槽正常工作。
相关文章:
- 在 Qt(C++) 中使用 QProcess 解压缩 - 提取目录问题
- 关于如何使用指向主窗口的指针的 QT 问题
- 如何解决"no Qt platform plugin could be initialized"问题?
- Qt Visual Studio 2015 加载项编译问题
- Qt OpenGL 渲染到纹理性能问题
- Qt 信号/插槽问题
- 在 armhf 上使用 cmake 在 qt 上的编译问题 "Syntax error: wird unexpected (expecting " ) " "
- 在 C++ 的 Qt 中使用 WINAPI 线程时出现问题
- Android因为权限问题而杀死Qt应用程序
- Qt & Firebase C++ SDK 在 iOS 上的链接问题
- 面临在 if 语句之外打印变量数据的问题 完成使用 Qt 编程
- CMake Qt建立在Windows链接器问题之上
- 需要有关在qt c ++应用程序中使用崩溃问题的建议
- 从 Qt 中的信号调用成员函数的问题
- Qt的新信号/时隙语法问题 - 连接到一个简单的函数
- 解决枚举命名问题 - Qt
- 为什么Q_OBJECT宏会导致问题(Qt)
- 影子变量问题Qt
- 时序问题:QT写入串行端口,然后读
- 本程序的信号和插槽问题(Qt)