0

I'm getting an error in a qt program where im trying to capture keystrokes. In the keyPressedEvent function in my qt program but i'm geting a weird error:

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

The class extends qmainwindow

heres my header file:

#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

here is my cpp file:

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

    }
}
Amre
  • 1,630
  • 8
  • 29
  • 41

1 Answers1

2

Did you read the compiler error? That's exactly what the problem is. You need to define keyPressEvent in your header file.

protected:
    void keyPressEvent(QKeyEvent *event);
Chris
  • 17,119
  • 5
  • 57
  • 60
  • I get 3 more errors: frenzywindow.cpp: In member function 'virtual void FrenzyWindow::keyPressEvent(QKeyEvent*)': frenzywindow.cpp:19:17: error: invalid use of incomplete type 'struct QKeyEvent' /usr/include/qt4/QtGui/qwidget.h:83:7: error: forward declaration of 'struct QKeyEvent' frenzywindow.cpp:34:18: error: invalid use of incomplete type 'struct QKeyEvent' /usr/include/qt4/QtGui/qwidget.h:83:7: error: forward declaration of 'struct QKeyEvent' make: *** [frenzywindow.o] Error 1 – Amre Dec 13 '12 at 00:55
  • 1
    You need a `#include ` in your cpp file – Chris Dec 13 '12 at 00:56
  • If i remove event from the declaration and the function, it compiles. But how do I get the key? – Amre Dec 13 '12 at 00:58