2012-12-13 50 views
0

我得到一个错误,在一个qt程序中,我试图捕获击键。在我的Qt程序keyPressedEvent功能但我歌厅一个奇怪的错误:keyPressedEvent错误Qt实现函数时

frenzywindow.cpp:16:50: error: no 'void FrenzyWindow::keyPressEvent(QKeyEvent*)' member function declared in class 'FrenzyWindow' 
make: *** [frenzywindow.o] Error 1 

的类继承的QMainWindow

继承人我的头文件:

#ifndef FRENZYWINDOW_H 
#define FRENZYWINDOW_H 

#include <QMainWindow> 
#include "frenzy.h" 

namespace Ui { 
class FrenzyWindow; 
} 

class FrenzyWindow : public QMainWindow 
{ 
    Q_OBJECT 

public: 
    explicit FrenzyWindow(QWidget *parent = 0); 
    ~FrenzyWindow(); 

signals: 
    void moveUp(); 
    void moveDown(); 
    void moveLeft(); 
    void moveRight(); 


private: 
    Ui::FrenzyWindow *ui; 
    Frenzy f; 
}; 

#endif // FRENZYWINDOW_H 

这里是我的CPP文件:

#include "frenzywindow.h" 
#include "ui_frenzywindow.h" 

FrenzyWindow::FrenzyWindow(QWidget *parent) : 
    QMainWindow(parent), 
    ui(new Ui::FrenzyWindow) 
{ 
    ui->setupUi(this); 
} 

FrenzyWindow::~FrenzyWindow() 
{ 
    delete ui; 
} 

void FrenzyWindow::keyPressEvent(QKeyEvent *event) 
{ 

    switch(event->key()) 
    { 
    case Qt::UpArrow: 
     emit moveUp(); 
     break; 
    case Qt::DownArrow: 
     emit moveDown(); 
     break; 
    case Qt::LeftArrow: 
     emit moveLeft(); 
     break; 
    case Qt::RightArrow: 
     emit moveRight(); 
     break; 
    default: 
      event->ignore(); 
      break; 

    } 
} 

回答

2

您是否看过编译器错误?这正是问题所在。您需要在头文件中定义keyPressEvent

protected: 
    void keyPressEvent(QKeyEvent *event); 
+0

我得到3个错误: frenzywindow.cpp:在成员函数 '虚拟无效FrenzyWindow :: keyPressEvent(QKeyEvent *)': frenzywindow.cpp:19:17:错误:无效使用不完整的类型的' struct QKeyEvent' /usr/include/qt4/QtGui/qwidget.h:83:7:error:'struct QKeyEvent'的前向声明' frenzywindow.cpp:34:18:error:invalid use of incomplete type'struct QKeyEvent' /usr/include/qt4/QtGui/qwidget.h:83:7:错误:'struct QKeyEvent'的前向声明 make:*** [frenzywindow.o]错误1 – Amre

+1

您需要#include 你的CPP文件 – Chris

+0

如果我是从声明和函数中移动事件,编译。但是,我如何获得钥匙? – Amre